views:

1820

answers:

7

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?

A: 

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
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
Ahhhhh! I understand now. Looks like you've already got a few good answers though.
Mark Biek
A: 

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
thanks but result is simply a string. i want a 4-element array ( what match() returns ) with the quotes stripped away
Scott Evernden
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
A: 

There are two parts to the fix for your problem, I think...

First, the .*? means "zero or more of any character, repeated zero or one times", which is an odd way of writing .*. More seriously, the 'any character' bit includes double quotes -- what you seem to be after is, therefore:

/"[^"]*"/g

This matches a double-quote, any string of characters excluding double quotes, and another double quote.

The second part is that you want to capture just the part inside the double quotes. In Perl or systems using PCRE (Perl-Compatible Regular Expressions) -- not sure whether Javascript falls into that category -- then you would use parentheses to capture the information you're after:

/"([^"]*)"/g

You then have to know how to get at the substrings - that varies from language to language, and I don't know enough Javascript to help at that point.

Jonathan Leffler
i thought .*? meant zero or more of any character, non-greedy / shortest match .. hmm
Scott Evernden
Scott is correct, .*? is the non-greedy version of .*
Greg Hewgill
+4  A: 

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
Bingo! .. works perfectly for my purpose! thanks David!
Scott Evernden
+1  A: 

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]
J.F. Sebastian
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
David's regexp is just fine but an alternative solution won't hurt a bit.
J.F. Sebastian
A: 

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
+1  A: 

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