【问题标题】:How can I find the duplicated characters of words between 2 sentences in javascript?如何在javascript中找到两个句子之间单词的重复字符?
【发布时间】:2017-09-22 11:58:42
【问题描述】:

如果第一个句子中的一个词包含在第二个句子中,是否有可能返回真或假?

示例 1:结果为真

  • 第一句话:“我叫阿里”
  • 第二句:“学生是:ali、josh、jone 和 tom”。

示例 2:结果为真

  • 第一:“老虎在这里”
  • 第二个:“狮子在这里

示例 3:结果为假

  • 第一:“开门”
  • 第二个:“窗口已关闭”

【问题讨论】:

  • 是的。尝试编写一些代码。
  • 是的,你可以。拆分你的字符串并尝试一下

标签: javascript filter


【解决方案1】:
function Match(s1, s2) {
        var a = s1.split(/[, :]+/).sort();
        var b = s2.split(/[, :]+/).sort();
        var ai = 0, bi = 0;

        while (ai < a.length && bi < b.length) {
            if (a[ai] < b[bi]) { ai++; }
            else if (a[ai] > b[bi]) { bi++; }
            else /* they're equal */
            {
                return true;
            }
        }

        return false;
    }

    var ss1 = 'my name is ali';
    var ss2 = 'students are: ali, josh, jone and tom';
    alert(Match(ss1, ss2));

【讨论】:

    【解决方案2】:

    回答

    简介

    可以轻松更改以下代码以满足您的需求。该代码通过使用一些特殊字符(即换行符)连接你的句子,然后使用正则表达式来测试你的字符串是否匹配。

    代码

    See this code in use here

    ^.*(\b\S+\b).*[\r\n].*(\1).*$
    

    下面的代码是从 regex101 自动生成的,以使用上面的 regex 代码。

    const regex = /^.*(\b\S+\b).*[\r\n].*(\1).*$/gmi;
    const str = `my name is ali
    students are: ali, josh, jone and tom
    tiger here
    The lion there
    open the door
    window is closed`;
    let m;
    
    while ((m = regex.exec(str)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            console.log(`Found match, group ${groupIndex}: ${match}`);
        });
    }

    注意事项

    • 我添加了不区分大小写的i 修饰符,以便在不考虑大小写的情况下捕获单词(即The 将匹配the
    • 正则表达式使用[\r\n] 作为字符串分隔符,您可以在此处使用任何内容,但出于查看目的,我使用\r\n 作为分隔符。
    • 如果您想确保只捕获完整的单词,您可以将(\1) 更改为(\b\1\b) - 这将确保here 不会匹配therehere 只会匹配这 4 个字符

    编辑

    只是为了演示获得结果所需的简单语法更改,我添加了此编辑。

    See this code in use here

    ^.*(\b\S+\b).*\|{3}.*(\1).*$
    

    const regex = /^.*(\b\S+\b).*\|{3}.*(\1).*$/gmi;
    const str = `my name is ali ||| students are: ali, josh, jone and tom
    tiger here ||| The lion there
    open the door ||| window is closed`;
    let m;
    
    while ((m = regex.exec(str)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            console.log(`Found match, group ${groupIndex}: ${match}`);
        });
    }

    注意:如果您不一次检查多个字符串 ((\b\S+\b).*\|{3}.*(\1)),则可以从开头删除 ^.* 并从末尾删除 .*$。添加这些只是为了针对您在问题中提出的每个字符串一次性演示此代码(发布 2 个链接而不是 6 个链接)。

    【讨论】:

      【解决方案3】:

      感谢您的解决方案。我根据需要使用了您的部分代码来创建自己的代码。

      searchByWords(s1, s2){
          if(s1==""){ //----------- In case s1 empty
              return true;
          }else{      //----------- In cass s1 not empty
              var array = s1.split(" ");
              for(int i=0; i<array.length; i++){
                  if(s2.toLowerCase().indexOf(array[i].toLowerCase())>=0){
                      return true;
                  }
              }
              return false;
          }
      }
      

      【讨论】:

        【解决方案4】:

        const f = (a, b) =>
        	a.split(' ').some(word =>
          	b.indexOf(word) !== -1)
        
        console.log(f('my name is ali', 'students are: ali, josh, jone and tom'))
        console.log(f('tiger here', 'The lion there'))
        console.log(f('open the door', 'window is closed'))

        【讨论】:

          【解决方案5】:

          你想要的是将一个句子逐个单词分解成一个数组,你可以使用 Javascript 的split() 函数来做到这一点。然后,只需比较数组。

          【讨论】:

          • 谢谢,我会尝试在我的代码中做到这一点。我希望得到正确的结果
          猜你喜欢
          • 1970-01-01
          • 2015-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-23
          • 2018-11-21
          • 2016-10-05
          相关资源
          最近更新 更多