简体   繁体   English

Javascript字符串匹配特定的正则表达式

[英]Javascript string match specific regex

I want to match specific string from this variable. 我想匹配此变量中的特定字符串。

var string = '150-50-30-20=50+skip50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+skip2*4*8=64';

Here is my regex : 这是我的正则表达式:

var string = '150-50-30-20=50+skip50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+skip2*4*8=64';

var match_data = [];

match_data = string.match(/[0-9]+(?:((\s*\-\s*|\s*\*\s*)[0-9]+)*)\s*\=\s*[0-9]+(?:(\s*\+\s*[0-9]+(?:((\s*\-\s*|\s*\*\s*)[0-9]+)*)\s*=\s*[0-9]+)*)/g);

console.log(match_data);

The output will show 输出将显示

[
   0: "150-50-30-20=50"
   1: "50-20-10-5=15+1*2*3*4=24+50-50*30*20=0"
   2: "2*4*8=64"
]

The result that I want to match from string variable is only 我要从string变量匹配的结果只是

 [
     0: "150-50-30-20=50"
     1: "1*2*3*4=24"
     2: "50-50*30*20=0"
 ]

You may use ((?:\\+|^)skip)? 您可以使用((?:\\+|^)skip)? capturing group before (\\d+(?:\\s*[-*\\/+]\\s*\\d+)*\\s*=\\s*\\d+) in the pattern, find each match, and whenever Group 1 is not undefined, skip (or omit) that match, else, grab Group 2 value. 在模式中的(\\d+(?:\\s*[-*\\/+]\\s*\\d+)*\\s*=\\s*\\d+)之前捕获组,找到每个匹配项,并且每当组1不存在时未定义,跳过(或忽略)匹配的内容,否则,获取组2的值。

 var string = '150-50-30-20=50+skip50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+skip2*4*8=64', reg = /((?:^|\\+)skip)?(\\d+(?:\\s*[-*\\/+]\\s*\\d+)*\\s*=\\s*\\d+)/gi, match_data = [], m; while(m=reg.exec(string)) { if (!m[1]) { match_data.push(m[2]); } } console.log(match_data); 

Note that I added / and + operators ( [-*\\/+] ) to the pattern. 请注意,我在模式中添加了/+运算符( [-*\\/+] )。

Regex details 正则表达式详细信息

  • ((?:^|\\+)skip)? - Group 1 (optional): 1 or 0 occurrences of +skip or skip at the start of a string -第1组(可选):在字符串开头出现1次或0次+skipskip
  • (\\d+(?:\\s*[-*\\/+]\\s*\\d+)*\\s*=\\s*\\d+) - Group 2: (\\d+(?:\\s*[-*\\/+]\\s*\\d+)*\\s*=\\s*\\d+) -组2:
    • \\d+ - 1+ digits \\d+ -1个以上数字
    • (?:\\s*[-*\\/+]\\s*\\d+)* - zero or more repetitions of (?:\\s*[-*\\/+]\\s*\\d+)* -零个或多个重复
      • \\s*[-*\\/+]\\s* - - , * , / , + enclosed with 0+ whitespaces \\s*[-*\\/+]\\s* -*/+包含0+空格
      • \\d+ - 1+ digits \\d+ -1个以上数字
    • \\s*=\\s* - = enclosed with 0+ whitespaces \\s*=\\s* - =包含0+空格
    • \\d+ - 1+ digits. \\d+ -1个以上的数字。

As per your input string and the expected results in array, you can just split your string with + and then filter out strings starting with skip and get your intended matches in your array. 根据您输入的字符串和数组中的预期结果,您可以使用+分割字符串,然后从skip过滤掉字符串,并在数组中获得预期的匹配项。

 const s = '150-50-30-20=50+skip50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+skip2*4*8=64' console.log(s.split(/\\+/).filter(x => !x.startsWith("skip"))) 

There are other similar approaches using regex that I can suggest, but the approach mentioned above using split seems simple and good enough. 我可以建议使用正则表达式的其他类似方法,但是上面提到的使用split的方法似乎简单且足够好。

try 尝试

var t = string.split('+skip');
var tt= t[1].split('+');
var r = [t[0],tt[1],tt[2]]

 var string = '150-50-30-20=50+skip50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+skip2*4*8=64'; var t = string.split('+skip'); var tt= t[1].split('+'); var r = [t[0],tt[1],tt[2]] console.log(r) 

 const string = '150-50-30-20=50+skip50-20-10-5=15+1*2*3*4=24+50-50*30*20=0+skip2*4*8=64'; const stepOne = string.replace(/skip[^=]*=\\d+./g, "") const stepTwo = stepOne.replace(/\\+$/, "") const result = stepTwo.split("+") console.log(result) 

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

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