Javascript Compatibility: Subpattern Return Only

"unescape('awefawef')unescape('efawefwf')unescape('awefawef')" .match(/unescape\(\'([^\)]+)\'\)/gi) 

If the number of matches unescape(...) matches it, it returns matches with unescape , but I only need what is inside the brackets () .

Thanks;)

Required Conclusion:

 ['awefawef','efawefwf','awefawef'] 
+4
source share
3 answers

You will need to use the ungreedy operator to match only the contents of the bracket and get all the results, you will need to call RegExp.exec several times on the same line.

 var regex = /unescape\('(.+?)'\)/ig; var string = "this unescape('foo') is a unescape('bar') test"; var result; while(result = regex.exec(string)) console.log(result[1]); 
+4
source

You can get it from RegExp.$1 .

 "unescape('awefawef')unescape('efawefwf')unescape('awefawef')".match(/unescape\(\'([^\)]+)\'\)/gi); console.log(RegExp.$1); //awefawef 
+1
source

If you want to use capture, you can use Regex.exec (). Using your case:

 var str = "unescape('foo')unescape('bar')unescape('baz')"; var regex = /unescape\(\'([^']+)\'\)/g; var result; while( result = regex.exec(str) ){ alert(result[1]); } 

Edited: Fixed original answer

+1
source

All Articles