按空格拆分字符串,保留带引号的段,允许转义引号 [英] Split a string by whitespace, keeping quoted segments, allowing escaped quotes
问题描述
我目前有这个正则表达式来按所有空格分割字符串,除非它在引用的段中:
I currently have this regular expression to split strings by all whitespace, unless it's in a quoted segment:
keywords = 'pop rock "hard rock"';
keywords = keywords.match(/\w+|"[^"]+"/g);
console.log(keywords); // [pop, rock, "hard rock"]
但是,我也希望它可以在关键字中引用,例如:
However, I also want it to be possible to have quotes in keywords, like this:
keywords = 'pop rock "hard rock" "\"dream\" pop"';
这应该返回
[pop, rock, "hard rock", "\"dream\" pop"]
什么是最简单的实现这一目标的方法?
What's the easiest way to achieve this?
推荐答案
您可以将正则表达式更改为:
You can change your regex to:
keywords = keywords.match(/\w+|"(?:\\"|[^"])+"/g);
而不是 [^] +
你有(?:\\| [^])+
允许 \
或其他角色,但不是未转义的引用。
Instead of [^"]+
you've got (?:\\"|[^"])+
which allows \"
or other character, but not an unescaped quote.
一个重要的注意事项是,如果你想让字符串包含一个文字斜杠,它应该是b e:
One important note is that if you want the string to include a literal slash, it should be:
keywords = 'pop rock "hard rock" "\\"dream\\" pop"'; //note the escaped slashes.
此外, \w + $ c $之间存在轻微的不一致c>和
[^] +
- 例如,它将匹配单词ab * d
,但不是 ab * d
(没有引号)。考虑使用 [^\ s] +
代替,这将匹配非空格。
Also, there's a slight inconsistency between \w+
and [^"]+
- for example, it will match the word "ab*d"
, but not ab*d
(without quotes). Consider using [^"\s]+
instead, that will match non-spaces.
这篇关于按空格拆分字符串,保留带引号的段,允许转义引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!