javascript REGex remove single quote in match - Stack Overflow

var RegTxt ="$f1$='test' AND f2='test2'";alert(RegTxt.match('[^

var RegTxt =  "$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.

var RegTxt =  "$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.

Share Improve this question edited Apr 6, 2010 at 14:22 skaffman 404k96 gold badges824 silver badges775 bronze badges asked Apr 6, 2010 at 14:20 mushtaqmushtaq 111 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745167495a4614721.html

相关推荐

  • javascript REGex remove single quote in match - Stack Overflow

    var RegTxt ="$f1$='test' AND f2='test2'";alert(RegTxt.match('[^

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信