Example :work "work & work
Result : ["work & work"]
Example : Exercise is "good" for "health" Result : ["good", "health"]
I wanted them in javascript
Example :work "work & work
Result : ["work & work"]
Example : Exercise is "good" for "health" Result : ["good", "health"]
I wanted them in javascript
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.
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"'));
var words = 'Exercise is "good" for "health"'.match(/(?<=")([\w]*?)(?=")/g);
console.log(words)