简体   繁体   English

使用正则表达式提取JavaScript中的字符串

[英]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. 我试图提取12为门数和onoff ,如两个不同的变量的状态。 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? 不确定如何提取on / off状态,是否有任何指针?

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)/ . 就其本身而言,可以使用/\\b(ON|OFF)\\b/找到ON / OFF,但是如果这些字符串中的其他位置可能存在ON / OFF,则可以使用/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;

Straight forward 直截了当

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

See a demo on regex101.com . 参见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: 如下所示,分别使用组$1$2

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

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. 然后,您可以使用onOrOff变量获取数据的字符串状态。

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' 输出: '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> 

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"] 这将输出一个包含数字和状态的数组: ["1", "ON"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM