在 javascript ( nodejs
) 中,我需要用 unicode 字符索引文本字符串,即给定一个字符串,如:
"Bonjour à tous le monde,
je voulais être le premier à vous dire:
-'comment ça va'
-<est-ce qu'il fait beau?>"
我想得到以下单词数组:
["Bonjour", "à", "tous", "le", "monde", "je", "voulais", "être", ... "beau"]
如何使用正则表达式或任何其他方式实现这一目标?
ps:我安装并尝试了 xregexp 模块,它为 javascript 提供了 unicode 支持,但是一般来说,正则表达式完全没用,我不能走得太远......
最佳答案
您可以使用 XRegExp bundled with addons 的版本(其中包括)添加了对正则表达式 unicode 类别的支持。我们对 not an unicode letter
类别感兴趣,即 \P{L}
。
然后,您可以通过正则表达式 XRegExp("\\P{L}+")
拆分字符串。
var s="Bonjour à tous le monde,\nje voulais être le premier à vous dire:\n -'comment ça va'\n -<est-ce qu'il fait beau?>";
var notALetter = XRegExp("\\P{L}+");
var words = XRegExp.split(s, notALetter);
见 this fiddle 。
关于javascript - 用unicode字符提取字符串中的单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20422194/