Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
6年前关闭。
我的具体问题是我想使用javascript拆分功能来拆分字符串,例如
我的输入字符串是:-
我期望我的输出像
我的剧本会像
还是有其他方法请帮助我。
谢谢!
您仍然必须过滤掉空的分割(由于“位于字符串的开头):
regexp匹配所有
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
6年前关闭。
我的具体问题是我想使用javascript拆分功能来拆分字符串,例如
我的输入字符串是:-
"Andy and sandy" are going
我期望我的输出像
var Result= ["Andy and sandy","are","going"];
我的剧本会像
var str='"Andy and sandy" are going';
var Result=str.split(RegularExp);//what would be the RegularExp ?
还是有其他方法请帮助我。
谢谢!
最佳答案
正则表达式可以是:
/ (?=(?:[^"]*"[^"]*")*[^"]*$)|"/
您仍然必须过滤掉空的分割(由于“位于字符串的开头):
var str = '"Andy and sandy" testing does work " lol is a nice word" are going';
var Result = str.split(/ (?=(?:[^"]*"[^"]*")*[^"]*$)|"/)
.filter(function(item) { return item !== '' });
// ["Andy and sandy", "testing", "does", "work", " lol is a nice word", "are", "going"]
regexp匹配所有
"
,并匹配任何空格,后跟偶数个"
。09-20 21:22