views:

200

answers:

4
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.

A: 

Here is a crude solution to your problem.

var match = RegTxt.match(/\'[^\']*'/g)
match = match.substring(1, match.length - 2);
Sohnee
It would be nice to know why the down-votes, based on the fact that this does functionally work and I did mention that it is a crude solution.
Sohnee
A: 
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.

Stefan Kendall
A: 

Trivial approach:

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

using your regex:

RegTxt.replace(/\'([^\']*)'/g, "$1")
Tomalak
+3  A: 

This would be quite simple if JavaScript supported negative lookbehinds:

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

But unfortunately, it doesn't.

In cases like these I like to *ab*use 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']
J-P
>>RegTxt should start with a lowercase 'r', as per convention.What Convention? This is very subjective and there are many opinions on this subject!
Gary Willoughby
J-P
yes your solution fixed the issue
mushtaq