目前,我在javascript中使用以下RegEx来匹配和计数字符串中的单词。它与这个ReEx一起工作:
RegEx:
var pickRegExp = /[^\W\d]+[\u00C0-\u017Fa-zA-Z'](\w|[-'](?=\w))*/gi;关键词:美丽
串:花园里那棵美丽的树。花园里有一棵美丽的树。
输出:
现在,我也想要匹配短语(准确)。例如:
关键词或短语:美丽的树
串:花园里那棵美丽的树。花园里有一棵美丽的树。这棵漂亮的模型树卖光了。
输出:
我对RegExp不是很坚定。你有什么窍门给我吗?谢谢
发布于 2014-09-25 06:29:19
关于
/\b(Beautiful tree|..*?)\b/gi例如,精确匹配和通用单词匹配regexp之间的逻辑OR?
s = ("The beautiful tree in the garden. In the garden is " +
"the beautiful tree. The model tree beautiful is sold out.");
result = {}
s.match(/\b(Beautiful tree|..*?)\b/gi).forEach(function(x) {
result[x] = (result[x]|0) + 1;
});给出
{ " ": 15,
". ": 2,
"In": 1,
"The": 2,
"beautiful": 1,
"beautiful tree": 2,
"garden": 2,
"in": 1,
"is": 2,
"model": 1,
"out": 1,
"sold": 1,
"the": 3,
"tree": 1 }https://stackoverflow.com/questions/26031775
复制相似问题