Reputation: 39926
in Javascript, the following:
var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result = test.match(/".*?"/g);
alert(result);
yields "the quick","brown fox","jumps over","the lazy dog"
I want each matched element to be unquoted: the quick,brown fox,jumps over,the lazy dog
what regexp will do this?
Upvotes: 10
Views: 19593
Reputation: 5546
For matching content between pairs of simple quotes and double quotes taking care of escaped ones.
As search engine first drove me here, I really would like to orient people looking to check quotes pairs to the more generic question: https://stackoverflow.com/a/41867753/2012407.
The regex will get the full content between well formed pairs of quotes like '"What\'s up?"'
for instance that are not in a code comment like // Comment.
or /* Comment. */
.
Upvotes: 0
Reputation: 39926
grapefrukt's answer works also. I would up using a variation of David's
match(/[^"]+(?=("\s*")|"$)/g)
as it properly deals with arbitrary amounts of white space and tabs tween the strings, which is what I needed.
Upvotes: 1
Reputation: 27045
This is what I would use in actionscript3:
var test:String = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result:Array = test.match(/(?<=^"| ").*?(?=" |"$)/g);
for each(var str:String in result){
trace(str);
}
Upvotes: 0
Reputation: 414345
It is not one regexp, but two simpler regexps.
var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result = test.match(/".*?"/g);
// ["the quick","brown fox","jumps over","the lazy dog"]
result.map(function(el) { return el.replace(/^"|"$/g, ""); });
// [the quick,brown fox,jumps over,the lazy dog]
Upvotes: 4
Reputation: 16257
This seems to work:
var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result = test.match(/[^"]+(?=(" ")|"$)/g);
alert(result);
Note: This doesn't match empty elements (i.e. ""). Also, it won't work in browsers that don't support JavaScript 1.5 (lookaheads are a 1.5 feature).
See http://www.javascriptkit.com/javatutors/redev2.shtml for more info.
Upvotes: 7
Reputation: 150799
You can use the Javascript replace() method to strip them out.
var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result = test.replace(/"/, '');
Is there more to it than just getting rid of the double-quotes?
Upvotes: 0
Reputation: 26384
Here's one way:
var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result = test.replace(/"(.*?)"/g, "$1");
alert(result);
Upvotes: -1