1

I am receiving strings from an external client and I am trying to perform regex-ing to extract a certain number and status that are embedded in this string. Here’s the examples of the strings that are coming in.

data = "< REP 1 INPUT_AUDIO_A ON >"
data = "< REP 1 INPUT_AUDIO_A OFF " 
data = "< REP 2 INPUT_AUDIO_A ON >" 
data = "< REP 2 INPUT_AUDIO_A OFF >"

and so on

I am trying to extract 1, 2 as gate number and the on or off as status in two different variables. I was able to extract numbers with regex as follows.

var gateNumberRegex = /[0-8]/g;
var gateNumber = data.charAt(data.search(gateNumberRegex)); //extract the gate number from string, output is 1 or 2 etc.

Not sure how to extract the on/off status, any pointers?

6
  • 2
    Is "INPUT_AUDIO_A" always gonna be there? Commented Jan 18, 2018 at 18:16
  • @GermanC Yes, it will always be there and if not, that string is not a match to perform regex-ing so I'd end up ignoring it Commented Jan 18, 2018 at 18:17
  • Then give it a shot with INPUT_AUDIO_A[\s]+([\w+]) Commented Jan 18, 2018 at 18:19
  • Try this Commented Jan 18, 2018 at 18:25
  • @GermanC: No need to put single expressions like \s or \w in square brackets, INPUT_AUDIO_A\s+(\w+) is equally fine and more readable. Commented Jan 18, 2018 at 18:28

5 Answers 5

1

Straight forward

(\d+)\s+INPUT_AUDIO_A\s+(ON|OFF)

See a demo on regex101.com.


Broken down, this says:

(\d+)         # capture 1+ digits into group 1
\s+           # match 1+ whitespaces
INPUT_AUDIO_A # INPUT_AUDIO_A literally
\s+           # 1+ whitespaces
(ON|OFF)      # ON or OFF

Use group $1 and $2, respectively as in this snippet:

var data = 'data = “< REP 1 INPUT_AUDIO_A ON >” or'
var match = data.match(/(\d+)\s+INPUT_AUDIO_A\s+(ON|OFF)/)
console.log(match[1])
console.log(match[2])

Sign up to request clarification or add additional context in comments.

1 Comment

Super cool! Need to learn regexes. Any resources to recommend?
1

By itself, ON/OFF may be found using /\b(ON|OFF)\b/ but if there may be ON/OFF in another place in those strings, you may go more contextual with /INPUT_AUDIO_A[\s]+(ON|OFF)/. Once you apply the regex like this

var match = `/\b(ON|OFF)\b/`.exec(yourLine)

you may extract the on/off bit with

var enabled = match ? match[1] : yourDefaultValue;

Comments

0

You could do something like this:

var onOrOffRegex = /INPUT_AUDIO_A ([^\s]+)/g
var onOrOff = data.match(onOrOffRegex)[1]

Then you could use the onOrOff variable to get the string status of the data.

Comments

0

For your very specific case, assuming the input is "valid", this is the most straight-forward solution:

\d+|ON|OFF

Output: '1','ON','1','OFF','2','ON','2','OFF'

And then you can separate it to "pairs":

var string = document.getElementById("input").innerHTML;
var array = string.match(/\d+|ON|OFF/g);
var result = [];
var print = "";
for (i = 0; i < array.length; i += 2) {
  result += [array[i], array[i + 1]];
  print += "[" + array[i] + ", " + array[i + 1] + "]<BR>"
}

document.getElementById("input").innerHTML = print;
<div id="input">
  data = “
    < REP 1 INPUT_AUDIO_A ON>” or data = “
    < REP 1 INPUT_AUDIO_A OFF>” or data = “
    < REP 2 INPUT_AUDIO_A ON>” or data = “
    < REP 2 INPUT_AUDIO_A OFF>”
</div>

Comments

0

The simplest way to do this would be:

var data = "< REP 1 INPUT_AUDIO_A ON >"

var result = data.match(/\b(\d+|ON|OFF)\b/g)

This will output an array containing the number and the status: ["1", "ON"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.