Cipher
Cipher

Reputation: 6102

Using regex to extract string in javascript

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?

Upvotes: 1

Views: 83

Answers (5)

jmcgriz
jmcgriz

Reputation: 3353

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"]

Upvotes: 0

GalAbra
GalAbra

Reputation: 5138

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>

Upvotes: 0

Jan
Jan

Reputation: 43199

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])

Upvotes: 1

TheOdd
TheOdd

Reputation: 265

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.

Upvotes: 0

YakovL
YakovL

Reputation: 8365

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;

Upvotes: 1

Related Questions