mushtaq
mushtaq

Reputation: 11

javascript REGex remove single quote in 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.

Upvotes: 1

Views: 9562

Answers (4)

James
James

Reputation: 111900

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']

Upvotes: 3

Tomalak
Tomalak

Reputation: 338178

Trivial approach:

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

using your regex:

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

Upvotes: 0

Stefan Kendall
Stefan Kendall

Reputation: 67812

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.

Upvotes: -1

Fenton
Fenton

Reputation: 250902

Here is a crude solution to your problem.

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

Upvotes: 1

Related Questions