-1

Given I have a sentence:

var testsentence = 'This "is" a wonderful "sentence" to "test" "stuff"';

How can I get an array like that?

var testarray = [ "is", "sentence", "test", "stuff" ]

UPDATE

I am using the Chromium console to try your response, but so far all the responses return:

[""is"", ""sentence"", ""test"", ""stuff""]

I don't want to have quotes in my matches.

user1680104
  • 7,657
  • 4
  • 20
  • 26

5 Answers5

2

To capture the quoted text, but not the quotes...note match won't return groups with the g modifier (see this question), so loop over the matches with something like:

var testsentence = 'This "is" a wonderful "sentence" to "test" "stuff"';
var pattern = /"([^"]+)"/g;
var match;
var testarray = [];
while(match = pattern.exec(testsentence)) {
    testarray.push(match[1]);
}
Community
  • 1
  • 1
Anson
  • 6,360
  • 2
  • 36
  • 31
  • Is this really the quickest way? Hoped there was something simpler. – user1680104 Dec 12 '12 at 17:15
  • Not sure....if you don't save the regex in `var pattern` then it won't iterate matches during each loop. If you don't mind keeping the quotes, then others' solutions are more brief. – Anson Dec 12 '12 at 17:28
1
(testsentence.match(/"\w+"/g) || []).map(function(w) {
    return w.slice(1, -1);
});
Bergi
  • 572,313
  • 128
  • 898
  • 1,281
  • Only match quoted words. `He said "well, I won't do it" abruptly` wouldn't match. – Clement Herreman Dec 12 '12 at 16:30
  • @ClementHerreman: should that match? I dont think so. For a solution that does this see burning_LEGION's and RicardoLohmann's answers. – Bergi Dec 12 '12 at 16:32
  • But I would have the quotes in the matches. – user1680104 Dec 12 '12 at 17:09
  • @user1680104: You'd have that with nearly all matching solutions. Just remove them from the results. – Bergi Dec 12 '12 at 17:20
  • Thank you. So there really isn't something simpler? What if I had a similar task with a non-static pattern before? like `/sequencepart\d+"(stufftofind)"/` – user1680104 Dec 12 '12 at 17:28
  • Exactly. You'd have to use the `exec()` loop (see Ansons' answer) or matching + removing quotes, as JavaScript does not support [lookbehind regexes](http://www.regular-expressions.info/lookaround.html) as in `/(?<=")\w+(?=")/g` – Bergi Dec 12 '12 at 17:38
  • How would I do something like `/sequencepart\d+"?(stufftofind)"?/` using Anson's answer? Is that possible? – user1680104 Dec 12 '12 at 23:20
  • I created [a new question](http://stackoverflow.com/questions/13850492/regular-expression-to-find-complex-markers), because it is. – user1680104 Dec 12 '12 at 23:31
  • @user1680104: Does Anson's code not work with that? It seems to be exactly the answer. – Bergi Dec 13 '12 at 00:12
1
testsentence.match(/"([^"\s])+"/g)

should return everything that is quoted, and avoid things like ""

Clement Herreman
  • 9,976
  • 4
  • 33
  • 57
1
testsentence.match(/"[^"]+"/g);

demo

Ricardo Alvaro Lohmann
  • 25,601
  • 7
  • 83
  • 81
0
return testsentence.match(/".+?"/g);
burning_LEGION
  • 12,864
  • 8
  • 38
  • 50