【问题标题】:JavaScript includes() and ignore no whitespace, fix code formattingJavaScript includes() 并且不忽略空格,修复代码格式
【发布时间】:2020-04-07 08:45:39
【问题描述】:

我有一个函数,它接受一个 string(特别是来自输入的地址或邮政编码),然后在匹配的结果周围添加强标签。它工作正常,但我希望它匹配没有空格的字符。例如,如果输入字符串是cf142,我希望它匹配CF14 2

下面是函数:

emboldenMatch(string, query) {
  query = query.toLowerCase().trim();
  if (string.toLowerCase().includes(query)) {
    const queryLength = query.length;
    const matchIndex = string.toLowerCase().indexOf(query);
    const matchEnd = matchIndex + queryLength;
    const before = string.substr(0, matchIndex);
    const match = string.substr(matchIndex, queryLength);
    const after = string.substr(matchEnd, string.length - matchEnd);  

    return `${before}<strong>${match}</strong>${after}`;
  } else {
    return string;
  }
}

【问题讨论】:

  • 您可以使用query = query.toLowerCase().trim().replace(/\s+/g, ''); 删除空格。那是你要找的吗? (解释:.replace() 是一个函数,它用第二个参数替换使用第一个参数找到的任何字符。第一个参数是匹配所有空白字符的正则表达式。第二个参数是一个空字符串)
  • 我认为这可能会弄乱索引位置。

标签: javascript string whitespace


【解决方案1】:

首先,您可以将您的存在函数编写如下:

 function emboldenMatch(string, query) {
    return string.replace(query, "<strong>$&</strong>")
}
console.log(em('some string with bold text', 'bold'));

如果您想包装每个匹配项,而不仅仅是第一个匹配项,并且不敏感,您可以将其设为 regexp,并添加 g (=global) 标志和 i (=insensitive ) 标志:

function emboldenMatch(string, query) {
  return string.replace(new RegExp(escapeRegExp(query), 'gi'), "<strong>$&</strong>")
}
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

console.log(emboldenMatch('some string with bold bold text', 'bold'));

现在,如果您想在字符之间添加可选空格,可以将其添加到正则表达式中,如下所示:

function emboldenMatch(string, query) {
    var reg = new RegExp(escapeRegExp(query).split('').join('\\s*'), 'gi');
    return string.replace(reg, "<strong>$&</strong>")
}
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

console.log(emboldenMatch('some string with bo ld  b old text', 'bold'));

【讨论】:

  • OPs 问题表明他们想要不区分大小写的匹配,所以添加 i 标志?
  • 现在这是一件美丽的事情。谢谢!
  • @YosefTukachinsky 您能否协助允许在查询中不包含逗号但在字符串中包含逗号的匹配项?谢谢
  • @YosefTukachinsky 你能帮忙解决我最后关于忽略逗号的评论吗?
  • 通过使用 .join('\\s*') 你忽略了空格,如果你想在那里添加逗号 - 只需添加那里。 .join('[\\s,]*')
【解决方案2】:

试试:

function emboldenMatch(string, query) {
    query = query.toLowerCase().trim().replace(/ /g,'');
    if (string.toLowerCase().includes(query)) {
      const queryLength = query.length;
      const matchIndex = string.toLowerCase().indexOf(query);
      const matchEnd = matchIndex + queryLength;
      const before = string.substr(0, matchIndex);
      const match = string.substr(matchIndex, queryLength);
      const after = string.substr(matchEnd, string.length - matchEnd);  

      return `${before}<strong>${match}</strong>${after}`;
    } else {
      return string;
    }
  }
  
  console.log(emboldenMatch('asdf2', 'asdf 2'))
  console.log(emboldenMatch('asdf2', 'asdf2'))

【讨论】:

    猜你喜欢
    • 2019-12-26
    • 2019-06-04
    • 1970-01-01
    • 2011-08-12
    • 2016-10-18
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多