我需要将一段文本处理成一个单词数组。
单词之间的分隔符是换行符、空格和各种标点符号,以及
.
我写的代码能够处理其他情况,但不能处理
情况。
注意:我需要处理相同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
// of below regex doesn't work
var word_array = text.split(/[ tnr.?,"';:!(){}<>/]| /)
console.log(text)
console.log(word_array)
}
</script>
</head><body>
<div id="text">this is text,break up test the words!ok</div>
</body></html>
问题是正则表达式将 看作正是这些字符。您需要使用'xa0'代替。