簡體   English   中英

如果開頭和(或)結尾有任何附件、字母、單詞,則與 angular 中的正則表達式不匹配

[英]Not to match with regex in angular if there are any attachments, letters, words at the beginning and(or) end

更新問題;

首先大家好,我是新來的,我會努力解決我的問題。

數據是如何找到我的;

    0: {highWord: 'hafif ticari araçlarda', color: '#00FF00'}
    1: {highWord: 'hafif ticari', color: '#00FF00'}
    2: {highWord: 'kamyon', color: '#00FFFF'}
    3: {highWord: 'MAN', color: '#00FFFF'}
    4: {highWord: 'otobüs', color: '#00FFFF'}
    5: {highWord: 'AĞIR VASITA', color: '#00FFFF'}
    6: {highWord: 'ağır vasıta', color: '#00FFFF'}
    7: {highWord: 'Ağır vasıta', color: '#00FFFF'}
    8: {highWord: 'Ağır Vasıta', color: '#00FFFF'}

這是我匹配我的話的代碼塊;

 let searchRgx = new RegExp(this.highlightWordList.map(item => {
      console.log(item)
      return item.highWord;
    }).join("|"), 'gi');

這是 output 這個代碼給了我

/hafif ticari araçlarda|hafif ticari|kamyon|MAN|otobüs|AĞIR VASITA|ağır vasıta|Ağır vasıta|Ağır Vasıta/gi

我在這里畫了匹配的單詞,但匹配錯誤

 let _this = this
  return txt.replace(searchRgx, function (match, offset, string)  {
     
      let foundedKeyword = _this.highlightWordList.filter(item => {
        return item.highWord.toLowerCase() === match.toLowerCase();
      })[0];

      if (foundedKeyword == undefined) {
        foundedKeyword = {};
        foundedKeyword.color = 'white';
      }


      return `<span style='background-color:${foundedKeyword.color};' >${match}</span>`;
    });

例如:當我寫“ lookman ”時,我上面的代碼也匹配“look man ”這個詞中的“ man

我想要的是,當我輸入"lookman"時,它與"man"不匹配。

我希望我提前問了正確的問題(有規則)謝謝

擴展凱利的評論

您可以添加單詞邊界\b 更新生成正則表達式的行

let searchRgx = new RegExp(this.highlightWordList.map(item => {
    console.log(item)       
    return '\\b(' + item.highWord + ')\\b'; 
    // Another string formatting option:  `\\b(${item.highWord})\\b`    
}).join("|"), 'gi');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM