-3

Example :work "work & work

Result : ["work & work"]

Example : Exercise is "good" for "health" Result : ["good", "health"]

I wanted them in javascript

Shahs
  • 1
  • 1
  • 1
    [StackOverflow isn't here to do your work for you](https://stackoverflow.com/help/on-topic). Show us what you have tried so far. We'll gladly help you. – Zenoo Dec 12 '17 at 10:19

4 Answers4

1

This answer is what you're looking for, including the snippet below with explanation for quick reference.

(["'])(?:(?=(\\?))\2.)*?\1

As an example using Javascript:

function getWordsBetweenQuotes(str) {
    return str.match(/(["'])(?:(?=(\\?))\2.)*?\1/g);
}

([""']) match a quote; ((?=(\?))\2.) if backslash exists, gobble it, and whether or not that happens, match a character; *? match many times (non-greedily, as to not eat the closing quote); \1 match the same quote that was use for opening.

melonlogic
  • 21
  • 2
1

You can do

function getResult(str){
    return str.split('"').filter((e, i) => (i&1))
}

console.log(getResult('work "work & work'));
console.log(getResult('Exercise is "good" for "health"'));
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

Maybe a regexp like this will work:

str.match(/\w+|"[^"]+"/g)
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
0

var words = 'Exercise is "good" for "health"'.match(/(?<=")([\w]*?)(?=")/g);

console.log(words)
adtanasa
  • 89
  • 4