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"
 ]
11
  • 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. Commented May 13, 2019 at 11:08
  • @WiktorStribiżew is that an answer? Commented May 13, 2019 at 11:14
  • With ECMAScript 2018 compliant environments, you may use match_data = string.match(/(?<!\+skip)\d+(?:\s*[-*]\s*\d+)*\s*=\s*\d+/gi) Commented May 13, 2019 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. Commented May 13, 2019 at 11:15
  • Can you explain your output please? Commented May 13, 2019 at 11:16

4 Answers 4

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.
Sign up to request clarification or add additional context in comments.

2 Comments

when i moved skip50-20-10-5=15 and the string will become var string = 'skip50-20-10-5=15+1*2*3*4=24+150-50-30-20=50+50-50*30*20=0+skip2*4*8=64'; it didn't work
@WillyantoHalim Easy fix, use reg = /((?:^|\+)skip)?(\d+(?:\s*[-*\/+]\s*\d+)*\s*=\s*\d+)/gi
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.

Comments

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)

4 Comments

this answer is only for 1 situation.. i want the code will run dynamically in any condition.
@WillyantoHalim you not desciribe desired output - describe it and give more examples in question
as you saw this post was about regex.. so i want to exclude it from regex.
@WillyantoHalim so you don't know what do you want on output? - regexp descibe wrong result so it is not good source of information what do you want
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)

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.