MAT14
MAT14

Reputation: 128

Regular Expression to match a pattern that contain some string except some string

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

Answers (2)

zx81
zx81

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 )...
  • and the * quantifier repeats that zero or more times
  • *\) matches the closing parenthesis

Upvotes: 1

anubhava
anubhava

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

Related Questions