如何通过在javascript中使用regex来删除空白,并通过点、逗号和换行来拆分字符串

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

本文关键字:字符串 拆分 换行 javascript 何通过 空白 删除 regex      更新时间:2023-09-26

我有这样的字符串:

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*/))

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