Regular expression to capture 3 strings in quotation marks separated by space
ok, I guess the code says it all.
//I have this string.
var str = '"Watch out" "for the" "rock!"'
//this is one of the开发者_Go百科 many patterns that I tried
res=str.match(/"(.*)" "(.*)" "(.*)"/g)
I want an array like: res=["Watch out","for the","rock!"]
How can I do that?
Like this:
var str = '"Watch out" "for the" "rock!"'
res=str.match(/"(.*?)" "(.*?)" "(.*?)"/)
res.shift()
You need to remove the g
flag because with that flag you will get a list of matches and not the list of groups for a single match. Also, you should specify your groups as non-greedy (quantifier *?
), otherwise they might match too much. Finally, remove the first entry from the array using res.shift()
- the first entry is the entire matched string and you want only the groups.
/"[^"]*"\s?/g
should be a better regex for splitting up any amount of values separated by quotations and spaces.
res = str.match(/^"([^"]*)" "([^"]*)" "([^"]*)"$/).slice(1)
About regular expression I can suggest you the following web site: RegEx LIB. It contains many regular expressions (the link I sent you is directly for String category).
Moreover you can test your regular expressions (also using Client Side engine - Javascript) at REG Tester.
I am sure there you can find your wished regular expression and you can also refer to it everytime you will need a regulare expression.
I use it every time I need RegEX in my projects.
Expanding on Ktash's answer, but using a capture group (the brackets) to get only the text within the quotation marks:
/"([^"]*)"\s?/g;
The only problem is that match()
doesn't return capture groups with a global modifier. So have to use in split()
or RegExp.exec()
which gets more messy:
Option 1 - Using RegExp.exec()
str = '"Watch out" "for the" "rock!"'
re = /"([^"]*)"\s?/g;
result = [];
match = re.exec(str);
while (match != null) {
result.push(match[1]);
match = re.exec(str);
}
Each call to re.exec(str)
returns the captured group until none are remaining when it will return null
. If you called it again, it would start again, i.e:
> str = '"Watch out" "for the" "rock!"'
""Watch out" "for the" "rock!""
> re = /"([^"]*)"\s?/g;
/"([^"]*)"\s?/g;
> re.exec(str)
[""Watch out" ", "Watch out"]
> re.exec(str)
[""for the" ", "for the"]
> re.exec(str)
[""rock!"", "rock!"]
> re.exec(str)
null
> re.exec(str)
[""Watch out" ", "Watch out"]
As per all the other answers here, the results returned are an array containing the whole match and then the capture group, hence why match[1]
is used.
Option 2 - Using split()
str = '"Watch out" "for the" "rock!"'
re = /"([^"]*)"\s?/; //The global identifier isn't needed
result = str.split(re)
result = result.filter(function(e){if (e!==""){return true}}) //Remove blanks from array
.split()
accepts a regex, however the results of str.split(re)
are:
["", "Watch out", "", "for the", "", "rock!", ""]
so it is necessary to use the filter()
function to remove the blank entries. I think the split()
approach is more elegant, but I think Internet Explorer (at least up to 8) lacks the filter()
function.
精彩评论