我尝试编写正则表达式将字符串拆分为数组。它必须使用空格或逗号的分隔符进行拆分,并忽略引用短语内的分隔符(使用单引号或双引号)。
到目前为止,我可以通过空格和逗号分隔它,但是我无法在引号之间忽略它们而且我迷失了。
var pattern = /\b\w+[^"', ]+(?!'")/g,
text = "Hello world \"Boston Red Sox\" hello, world, \'boston, red sox\', \'beached whale\', pickup sticks",
output = text.match(pattern);
当前输出:
["Hello", "world", "Boston", "Red", "Sox", "hello", "world", "boston", "red", "sox", "beached", "whale", "pickup", "sticks"]
期望的输出:
["Hello", "world", "Boston Red Sox", "hello", "world", "boston, red sox", "beached whale", "pickup", "sticks"]
任何帮助都会很棒!
答案 0 :(得分:5)
只需使用|
var regex = /"([^"]*)"|'([^']*)'|[^\s,]+/g;
var text = "Hello world \"Boston Red Sox\" hello, world, \'boston, red sox\', \'beached whale\', pickup sticks";
var output = [];
var m;
while ((m = regex.exec(text)) !== null)
{
output.push(m[1] || m[2] || m[0]);
}
console.log(output);