简体   繁体   English

如何在JavaScript中使用正则表达式按点,逗号和换行符分隔字符串,并删除空格?

[英]How can i split a string by dot, comma and newline with removing whitespaces by using regex in javascript?

I have string like this: 我有这样的字符串:

what, be inclined to, hi . hello 
where

I want to split it like this: 我想这样分割它:

["what","be inclined to","hi","hello","where"]

Currently I'm using this regex but It doesn't work like I want: 目前,我正在使用此正则表达式,但无法正常运行:

input_words.val().replace(/^\s*|\s*$/g, '').split(/\n|\s*,|\./);

split function alone would be enough. 仅使用split函数就足够了。 The below regex would split your input according to one or more commas or dots or newline characters along with the preceding or following zero or more spaces. 下面的正则表达式将根据一个或多个逗号,点或换行符以及前面或后面的零个或多个空格来分割输入。

 var s = "what, be inclined to, hi . hello\\nwhere"; alert(s.split(/\\s*[,\\n.]+\\s*/)) 

var test ="what, be inclined to, hi . hello\nwhere";
var elements = test.split(/[,\n.\s+]+/) || [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM