Javascript的正则表达式如何同时过滤具有多个规则的字符串?



我需要将一段文本处理成一个单词数组。

单词之间的分隔符是换行符、空格和各种标点符号,以及 .

我写的代码能够处理其他情况,但不能处理 情况。

注意:我需要处理相同regex内的所有情况,不能用空格替换 


这段代码不会错的,它只是运行在结果不是预期的值

在生成的单词数组中,"break up test the words"是一个值(错误),我需要它为5:[break,up,test,the,words](正确)


我的代码:

<!DOCTYPE html><html><head>
<script>
window.onload = function(){
var text = document.getElementById('text').textContent
// &nbsp; of below regex doesn't work
var word_array = text.split(/[ tnr.?,"';:!(){}<>/]|&nbsp;/)
console.log(text)
console.log(word_array)
}
</script>
</head><body>
<div id="text">this   is text,break&nbsp;up&nbsp;test&nbsp;the&nbsp;&nbsp;words!ok</div>
</body></html>

问题是正则表达式将&nbsp看作正是这些字符。您需要使用'xa0'代替。

最新更新