开发者

javascript REGex remove single quote in match

var RegTxt 开发者_StackOverflow社区=  "$f1$='test' AND f2='test2'";
alert(RegTxt.match(/\'[^\']*'/g))

returns the match correctely i:e 'test','test2' but how can i remove the single quote in the match.


This would be quite simple if JavaScript supported negative lookbehinds:

/(?<=').*?(?=')/

But unfortunately, it doesn't.

In cases like these I like to abuse String.prototype.replace:

// btw, RegTxt should start with a lowercase 'r', as per convention
var match = [];
regTxt.replace(/'([^']*)'/g, function($0, $1){
    match.push($1);
});
match; // => ['test', 'test2']


Here is a crude solution to your problem.

var match = RegTxt.match(/\'[^\']*'/g)
match = match.substring(1, match.length - 2);


Trivial approach:

RegTxt.replace(/'/g, "")

using your regex:

RegTxt.replace(/\'([^\']*)'/g, "$1")


var matches = str.match(regex);
var newMatches = [];
for( i in matches )
{
var word = matches[i];
newMatches.push( word.substring(1,word.length-1))
}

newMatches will now contain the array you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜