【问题标题】:Optamise way to match a word in javascript?优化在javascript中匹配单词的方法?
【发布时间】:2020-11-14 22:09:02
【问题描述】:

如果我每次遇到 区分大小写时都使用 'if''split' 方法,我会尝试匹配 javascript 中的单词强>。

如果我使用的是 regExp,那么如果单词的一部分匹配它返回 true (即:hii,/hi/)。我所做的匹配不区分大小写和整个单词应该匹配。

正则表达式代码

async function matchOne(str ,mtc) {
  let returnval;
  let words = str.split(' ')
  await words.forEach(word => {
    if (word.match(mtc)) {
      returnval = true;
      return true
    }
  });
  if (returnval === true) {
    return true;
  } else {
    return false;
  }
}
matchOne(string,regEx)

if 语句代码

async function matchOne(str ,mtc) {
  let returnval;
  let words = str.split(' ')
  await words.forEach(word => {
    if (word == mtc) {
      returnval = true;
      return true
    }
  });
  if (returnval === true) {
    return true;
  } else {
    return false;
  }
}

matchOne(string,string)

【问题讨论】:

    标签: javascript regex string split string-matching


    【解决方案1】:

    你好,看看下面的代码 sn-p 希望对你有帮助

    const str = 'arya stark';
    
    // The most concise way to check substrings ignoring case is using
    // `String#match()` and a case-insensitive regular expression (the 'i')
    str.match(/Stark/i); // true
    str.match(/Snow/i); // false
    

    如果您想使用 forEach,请考虑将字符串转换为小写并搜索单词的小写版本,如下所示

    str.toLowerCase().includes('Stark'.toLowerCase()); // true
    str.toLowerCase().indexOf('Stark'.toLowerCase()) !== -1; // true
    

    【讨论】:

    • What I do do to match case-insensitive and whole word should be match. 这也能解决整个单词吗?示例 the boy spoke starkly 不应匹配。
    【解决方案2】:

    您可以使用正则表达式匹配字符串并使用全局和不区分大小写的标志

    let str = 'this string have Capital and no capital';
    
    let match = str.match(/capital/gi);
    
    console.log(match);

    【讨论】:

      【解决方案3】:

      我认为这对你有用。

      为了清楚起见,我删除了一些代码。

      function matchOne(str ,mtc) {
        let returnval;
        let words = str.split(' ')
        words.forEach(word => {
          console.log(word)
          if (word.toLower().match(mtc)) {
            console.log('Found word:'+ word + ' regexp='+mtc)
            returnval = true;
          } else console.log('NOT Found word:'+ word  + ' regexp='+mtc)
        });
        return returnval;
      }
      matchOne('Hello world','he'); // true
      matchOne('Hello world','He'); // true
      

      至于大写/小写。您需要决定 input & regexp 应该是大写还是小写等等。 只需将 .toLower() 附加到您的论点即可。

      例如:word.toLower().match(mtc)

      【讨论】:

        【解决方案4】:
        1. 正则表达式匹配

        考虑到正则表达式将匹配整个字符串,因此不需要将字符串拆分为单词。

        const regex = /hello/gi
        const string = "hello world"
        console.log(!!string.toLowerCase().match(regex)); // true
        
        const string2 = "HELLO WORLD"
        console.log(!!string2.toLowerCase().match(regex)); // true
        
        const string3 = "WORLD ABC"
        console.log(!!string3.toLowerCase().match(regex)); // false
        

        这里i 使正则表达式匹配不区分大小写。 !! 用于将匹配后我们收到的数组转换为布尔值。

        1. 字符串匹配

        您可以进一步简化和清理您的代码 - 类似这样。 我们按单词拆分字符串。这将为我们提供一系列单词。现在我们检查是否至少有一个词与您的匹配项相等。

        function matchOne(string, mtc) {
          return string.split(' ').some(word => word.toLowerCase() === mtc.toLowerCase())
        }
        
        console.log('hello world', 'hello'); // true
        console.log('world abc', 'hello'); // false
        
        

        【讨论】:

        • 还有一些关于你的代码 sn-p 的 cmets。 1. 这里 forEach 不需要async-await。 2、forEach内不需要返回值。在这种情况下,请使用map。在这里阅读更多:stackoverflow.com/questions/34426458/…
        • 我添加了返回值,因为我只想在条件匹配时停止循环。在这里我使用 split 方法,如果最后一个单词不匹配,那么它将 returnval 的值设置为 false。而且我不想要它,所以我这样做是为了停止循环
        • 我认为它会起作用:- async function matchOne(str ,mtc) { let returnval; let words = str.split(' ') await words.forEach(word => { if (word.toLowerCase() == mtc.toLowerCase()) { returnval = true; return true; } }); if (returnval === true) { return true; } else { return false; } } matchOne(string,string)
        • " 如果条件匹配,我只想停止循环" => 你不能打破 forEach。阅读更多:stackoverflow.com/questions/6260756/…
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 2020-01-04
        • 2011-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多