【问题标题】:Find words from array in string, whole words only (with hebrew characters)从字符串中的数组中查找单词,仅整个单词(带有希伯来字符)
【发布时间】:2017-01-12 08:16:52
【问题描述】:

我必须构建一个 RegExp 对象,它将从数组中搜索单词, 并且只会发现整个单词匹配。

例如 我有一个单词数组('יל','ילד'), 我希望 RegExp 找到“a”或“יל”或“ילד”,而不是“ילדד”。

这是我的代码:

var text = 'ילד ילדדד יל';
var matchWords = ['יל','ילד'];
text = text.replace(/\n$/g, '\n\n').replace(new RegExp('\\b(' + matchWords.join('|') + ')\\b','g'), '<mark>$&</mark>');
console.log(text);

我尝试过的:

我试过这段代码:

new RegExp('(יל|ילד)','g');

效果很好,但它也能找到像“ילדדדד”这样的词,我只需要匹配整个词。

我也试过这段代码:

new RegExp('\\b(יל|ילד)\\b','g');

但是这个正则表达式没有找到任何单词!

我应该如何构建我的 RegExp?

【问题讨论】:

  • new RegExp('\\b(word1|word2|word3)\\b','g'); 怎么找不到匹配项?请提供代码sn-p进行复现。
  • 这是我的代码:text = text.replace(/\n$/g, '\n\n').replace(new RegExp('\\b(' + matchWords.join('|') + ')\\b','g'), '&lt;mark&gt;$&amp;&lt;/mark&gt;');
  • 将代码添加到问题中。
  • 所以,您只需将最后一个换行符加倍,但matchWords 变量包含什么?请连同text 变量一起添加到问题中。
  • 你看,我把代码变成了sn-p,你得到的结果是word1111 &lt;mark&gt;word1&lt;/mark&gt; wwword2。它有效,不是吗?

标签: javascript regex


【解决方案1】:

单词边界\b 不支持Unicode。使用XRegExp 构建Unicode 字边界:

var text = 'ילד ילדדד יל';
var matchWords = ['יל','ילד'];
re = XRegExp('(^|[^_0-9\\pL])(' + matchWords.join('|') + ')(?![_0-9\\pL])','ig');
text = XRegExp.replace(text.replace(/\n$/g, '\n\n'), re, '$1<mark>$2</mark>');
console.log(text);
&lt;script src="http://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.min.js"&gt;&lt;/script&gt;

这里,(^|[^_0-9\\pL]) 是一个 ID=1 的捕获组,它匹配字符串 start 或除 Unicode 字母、ASCII 数字或 _(前导词边界)以外的任何字符,而 (?![_0-9\\pL]) 匹配失败如果单词后面跟着_、ASCII 数字或 Unicode 字母。

借助现代 ECMAScript 2018+ 标准支持,您可以使用

let text = 'ילד ילדדד יל';
const matchWords = ['יל','ילד'];
const re = new RegExp('(^|[^_0-9\\p{L}])(' + matchWords.join('|') + ')(?![_0-9\\p{L}])','igu');
text = text.replace(re, '$1<mark>$2</mark>');
console.log(text);

另一个完全模拟 Unicode 感知 \b 构造的ECMAScript 2018+ 兼容解决方案在Replace certain arabic words in text string using Javascript 进行了解释。

【讨论】:

    【解决方案2】:

    //Words to join
    var words = ['apes', 'cats', 'bazooka'];
    //String to search
    var str = 'it\'s good that cats and dogs dont wear bazookas';
    //End at start of line, end of line or whitespace
    var end = '(^|$|\\s)';
    //Regular expression string
    var regex = end + "(" + words.join('|') + ")" + end;
    //Build RegExp
    var re = new RegExp(regex, "gi");
    //Log results
    console.log(str.match(re));

    或作为函数

    var findWholeWordInString = (function() {
      //End at start of line, end of line or whitespace
      var end = '(^|$|\\s)';
      //The actual function
      return function(str, words) {
        //Regular expression string
        var regex = end + "(" + words.join('|') + ")" + end;
        //Build RegExp
        var re = new RegExp(regex, "gi");
        //Return results
        return str.match(re);
      };
    })();
    //Run test
    console.log(findWholeWordInString('it\'s good that cats and dogs dont wear bazookas', ['apes', 'cats', 'bazooka']));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多