Reputation: 128
I have string like following:
var string = "'abc'+@VAL([Q].[F1 w1 w1 w1])+ 'hellllllllllo'+@VAL([Q].[F2 w]) +'anything'+@VAL([Q].[F3 w])+anything";
i want to match the following pattern
@VAL([Q].[F1 w1 w1 w1])
@VAL([Q].[F2 w])
@VAL([Q].[F3 w])
actually i want to match a string pattern start with @VAL( and end with ) I have tried with the following code:
var patt1 = /@VAL\((.*)\)/g;
var myPattern = string.match(patt1);
and get the following result
@VAL([Q].[F1 w1 w1 w1])+ 'hellllllllllo'+@VAL([Q].[F2 w]) +'anything'+@VAL([Q].[F3 w])
i could not sort it out need help!!!!!!!!!
Upvotes: 1
Views: 80
Reputation: 41838
This will return all the matches:
result = subject.match(/@VAL\([^)]*\)/g);
See demo.
@VAL\(
matches @VAL(
[^)]
matches one character that is not a )
...*
quantifier repeats that zero or more times*\)
matches the closing parenthesisUpvotes: 1
Reputation: 785531
Your regex is too greedy, Use this negation based regex:
var patt1 = /@VAL\(([^)]*)\)/g;
OR just make it lazy:
var patt1 = /@VAL\((.*?)\)/g;
var m = string.match(patt1);
//=> ["@VAL([Q].[F1 w1 w1 w1])", "@VAL([Q].[F2 w])", "@VAL([Q].[F3 w])"]
Upvotes: 0