我有这样的字符串:
what, be inclined to, hi . hello
where
我想这样分割它:
["what","be inclined to","hi","hello","where"]
目前我正在使用这个正则表达式,但它并不像我想要的那样工作:
input_words.val().replace(/^\s*|\s*$/g, '').split(/\n|\s*,|\./);
最佳答案
单独的
split
函数就足够了。下面的正则表达式将根据一个或多个逗号、点或换行符以及前面或后面的零个或多个空格来分割您的输入。
var s = "what, be inclined to, hi . hello\nwhere";
alert(s.split(/\s*[,\n.]+\s*/))
关于javascript - 如何使用 javascript 中的正则表达式按点、逗号和换行符拆分字符串并删除空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31230890/