2

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

 [
     0: "150-50-30-20=50"
     1: "1*2*3*4=24"
     2: "50-50*30*20=0"
 ]
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
Willyanto Halim
  • 413
  • 1
  • 6
  • 19
  • You may use `/(\+skip)?(\d+(?:\s*[-*]\s*\d+)*\s*=\s*\d+)/gi` and whenever Group 1 is not undefined, skip/omit that match, else, grab Group 2 value. See [this **JS demo**](https://jsfiddle.net/wiktor_stribizew/mgtqhvp8/2/). – Wiktor Stribiżew May 13 '19 at 11:08
  • @WiktorStribiżew is that an answer? – evolutionxbox May 13 '19 at 11:14
  • With ECMAScript 2018 compliant environments, you may use `match_data = string.match(/(?<!\+skip)\d+(?:\s*[-*]\s*\d+)*\s*=\s*\d+/gi)` – Wiktor Stribiżew May 13 '19 at 11:14
  • @evolutionxbox No idea, probably it is an answer. I am just not sure about the actual requirement. If something works, it is not necessarily the answer. – Wiktor Stribiżew May 13 '19 at 11:15
  • Can you explain your output please? – evolutionxbox May 13 '19 at 11:16
  • I got what you meant.. but i will try it first. @WiktorStribiżew. and for negative lookbehind that you answered on second comment, it will not support on any browser except chrome. – Willyanto Halim May 13 '19 at 11:16
  • @WillyantoHalim That is why the second comment starts with *With ECMAScript 2018 compliant environments*... Please confirm you want to exract all sums that are not preceded with `+skip`. Do you only want to support `-` and `*` or all operations? Maybe relace `[-*]` with `[-*\/+]`, too. – Wiktor Stribiżew May 13 '19 at 11:17
  • @WiktorStribiżew yes i know, so that i won't choose the second comment.. i just want to exclude +skip only but with dynamically code. so that in any condition, it could be run. – Willyanto Halim May 13 '19 at 11:21
  • So, https://jsfiddle.net/wiktor_stribizew/mgtqhvp8/2/ is what you need – Wiktor Stribiżew May 13 '19 at 11:24
  • okay.. just put yours on answer section.. i will mark it as true answer if i have done checking. – Willyanto Halim May 13 '19 at 11:28
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you . – Wiktor Stribiżew May 13 '19 at 18:54

4 Answers4

2

You may use ((?:\+|^)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.

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
  • (\d+(?:\s*[-*\/+]\s*\d+)*\s*=\s*\d+) - Group 2:
    • \d+ - 1+ digits
    • (?:\s*[-*\/+]\s*\d+)* - zero or more repetitions of
      • \s*[-*\/+]\s* - -, *, /, + enclosed with 0+ whitespaces
      • \d+ - 1+ digits
    • \s*=\s* - = enclosed with 0+ whitespaces
    • \d+ - 1+ digits.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

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.

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.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
0

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)
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
0

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)
Pablo
  • 2,137
  • 1
  • 17
  • 16