【问题标题】:React highlighting query [closed]反应突出显示查询[关闭]
【发布时间】:2021-04-17 14:41:40
【问题描述】:

我正在制作一个建议组件,我需要突出显示与查询匹配的建议部分(就像 duckduck.go 一样)。

当前逻辑

function Highlight({ text = '', highlight = '' }) {
  const regex = new RegExp(`(${highlight})`, 'gi');
  const split = text.split(regex);
  return (
    <span>
      {split
        .filter((current) => current)
        .map((current, i) => (regex.test(current) ? <span key={i}>{current}</span> : <em>key={i}>{current}</em>))}
    </span>
  );
}

例如
建议:美国、联合航空公司
查询:联合 美国 美国/United Airlines ## 工作正常
查询:United St United States / United Airlines ## 工作不正常
期待:United St ==> United States & United Airlines
期待:nited St ==> United States & United Airlines
期待:Unit St ==> United States 和联合航空公司

【问题讨论】:

  • 您能解释一下您需要实现什么行为吗?

标签: javascript reactjs regex search


【解决方案1】:

已修复。

更改

const regex = new RegExp(`(${highlight})`, 'gi');

const regex = new RegExp(
    `\\b(${escapeRegExp(phrase)
      .trim()
      .split(' ')
      .filter((t) => t)
      .join('|')})`,
    'gi'
  );

 1. (\\b(${escapeRegExp(phrase).trim().split(' ').filter((t) =>
    t).join('|')})`,'gi');                                     ===> left side must match

 2.  (${escapeRegExp(phrase).trim().split(' ').filter((t) =>
    t).join('|')})\\b`,'gi');                                  ===> right side must match 

 3. (\\b(${escapeRegExp(phrase).trim().split(' ').filter((t) =>
    t).join('|')})\\b`,'gi');                                  ===> both sides must match

 4. ((${escapeRegExp(phrase).trim().split(' ').filter((t) =>
    t).join('|')})`,'gi');                                     ===> neither side has to match

【讨论】:

    【解决方案2】:

    我刚刚写了一个符合你要求的函数

    请注意此函数区分大小写 打字稿

    const highLight = (
      query: string, // The Query
      suggestion: string, // The Suggestion
      tag: string = "strong"
    ): string => {
      const querySplit = query.split(" ");
      const suggestionSplit = suggestion.split(" ");
    
      let res: string = "";
      let matchFullFirst = false; // This Variable track if the first Word is a complete match 
    
      for (let i = 0; i < querySplit.length && i < suggestionSplit.length; i++) {
        if (suggestionSplit[i].startsWith(querySplit[i])) {
          if (i == 0 && suggestionSplit[i].length === querySplit[i].length) {
            matchFullFirst = true;
          }
    
          res += ` <${tag}>${querySplit[i]}</${tag}>${suggestionSplit[i].slice(
            querySplit[i].length
          )}`;
          continue;
        }
    
        if (matchFullFirst) {
          res += suggestion.slice(querySplit[0].length);
          break;
        }
        res = suggestion;
        break;
      }
      return res;
    };
    

    Javascript

      const highLight = (query, suggestion, tag = "strong") => {
      const querySplit = query.split(" ");
      const suggestionSplit = suggestion.split(" ");
    
      let res = "";
      let matchFullFirst = false;
      
      for (let i = 0; i < querySplit.length && i < suggestionSplit.length; i++) {
        if (suggestionSplit[i].startsWith(querySplit[i])) {
          if (i == 0 && suggestionSplit[i].length === querySplit[i].length) {
            matchFullFirst = true;
          }
    
          res += ` <${tag}>${querySplit[i]}</${tag}>${suggestionSplit[i].slice(
            querySplit[i].length
          )}`;
          continue;
        }
    
        if (matchFullFirst) {
          res += suggestion.slice(querySplit[0].length);
          break;
        }
        res = suggestion;
        break;
      }
      return res;
    };
    

    【讨论】:

    • United St => 仅突出显示 'United States' Nited States => 不突出任何内容
    • 请再次检查问题,该功能符合您在问题中提到的所有期望,
    • 我觉得有点混乱,你能不能编辑一下预期,把它们变成粗体而不是斜体,因为它是混乱的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2018-05-20
    • 2012-08-01
    相关资源
    最近更新 更多