admin管理员组

文章数量:1313777

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

If there multiple unescape(...) matches it returns matches with unescape, but i need only what is inside () brackets.

Thanks ;)

Desired output:

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

If there multiple unescape(...) matches it returns matches with unescape, but i need only what is inside () brackets.

Thanks ;)

Desired output:

['awefawef','efawefwf','awefawef']
Share Improve this question edited Aug 4, 2011 at 16:57 Somebody asked Aug 4, 2011 at 16:24 SomebodySomebody 9,64526 gold badges98 silver badges144 bronze badges 1
  • Sure, added to the main post. – Somebody Commented Aug 4, 2011 at 16:57
Add a ment  | 

3 Answers 3

Reset to default 5

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

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]);

You can get it from RegExp.$1.

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

If you want to use capturing, 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 up original answer

本文标签: Javascript match return only subpattern matched contentsStack Overflow