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
3 Answers
Reset to default 5You 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
版权声明:本文标题:Javascript match: return only subpattern matched contents - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741893864a2403460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论