10

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?

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84

7 Answers7

7

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.

David Crow
  • 16,077
  • 8
  • 42
  • 34
4

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] 
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Yeah i can see how this would work, but I sorta figure if I am going to plant some ugly-ass regexp stuff in my code, it oughta be worth it by do everything i want in one shot. :) – Scott Evernden Sep 29 '08 at 06:21
1

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.

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
0

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?

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
  • i specifically want the result array that match() returns. i know i can trim each element, but i'm sure there's a way to do it one-step using a crafty regular expression – Scott Evernden Sep 29 '08 at 05:50
0

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);
}
grapefrukt
  • 27,016
  • 6
  • 49
  • 73
0

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. */.

Community
  • 1
  • 1
antoni
  • 5,001
  • 1
  • 35
  • 44
-1

Here's one way:

var test = '"the quick" "brown fox" "jumps over" "the lazy dog"';
var result = test.replace(/"(.*?)"/g, "$1");
alert(result);
Gordon Wilson
  • 26,244
  • 11
  • 57
  • 60
  • thanks but result is simply a string. i want a 4-element array ( what match() returns ) with the quotes stripped away – Scott Evernden Sep 29 '08 at 05:54
  • Ah, sry. My javascript's a bit rusty. Looks like there are no regex lookbehinds in javascript. So David's answer is your best bet. – Gordon Wilson Sep 29 '08 at 06:14