简体   繁体   English

确定两个字符串之一是否是另一个String Javascript的子字符串?

[英]Determining if one of the two strings is a substring of the other String Javascript?

Suppose I have two text fields where the user can enter any text. 假设我有两个文本字段,用户可以在其中输入任何文本。

One field can have: The sky is blue. 一个字段可以具有: The sky is blue.
Second field have: The ocean is blue. 第二场有: The ocean is blue.

In Javascript what is the easiest way to check if one string entered in one field is a substring for the other field? 在Javascript中,最简单的方法是检查在一个字段中输入的一个字符串是否是另一个字段的子字符串?

So far this is what I have: 到目前为止,这就是我所拥有的:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title>Substring Test</title>
      <script type="text/javascript"> 

         function displayContent() {
         var contentEntered = document.getElementById("userContent").value; 
         var contentEnteredSecond = document.getElementById("secondUserContent").value;
               document.getElementById("result").innerHTML += "<p> The content entered was " + contentEntered + " </p>"; 
               document.getElementById("result").innerHTML += "<p> The content entered was " + contentEnteredSecond + " </p>"; 
         }

      </script>
   </head>
   <body>
      <h1>Substring</h1>
      <div id="mainCont">
         <p>Please enter any content.</p>
         <p>Content:
            <input type="text" id="userContent">
         </p>
         <p>Content:
            <input type="text" id="secondUserContent">
         </p>
         <p>
            <input type="button" onclick="displayContent();" value="Submit">
         </p>
         <div id="result"></div>
      </div>
   </body>
</html>
function checkSub(str1, str2){

    if(str1.indexOf(str2) > -1 || str2.indexOf(str1) > -1) return true;
    else return false;
}

在ES6中,使用String.prototype.includes很简单:

str1.includes(str2) || str2.includes(str1);

You can use includes function from lodash library. 您可以使用includes从功能lodash库。

function checkSubstr(str1, str2) {
    return _.includes(str1, str2) || _.includes(str2, str1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 substring函数使用javascript分隔两个字符串 - substring function to separate two strings using javascript Javascript REGEX,用于查找两个字符串之间的子字符串 - Javascript REGEX for finding a substring between two strings 用javascript中的正则表达式匹配其他两个字符串之间的字符串 - Match a string between two other strings with regex in javascript 如何检查一个javascript字符串是否在其他两个字符串之间 - How to check if a javascript string is between two other strings 比较 JavaScript 中的字符串时,为什么一个字符串大于另一个字符串? - Why is one string greater than the other when comparing strings in JavaScript? 确定两个JavaScript字符串是否为字谜的代码背后的逻辑 - Logic behind code determining if two javascript strings are anagrams 确定用javascript String.fromCharCode()解析的空字符串是否相等 - determining equality for empty strings parsed with javascript String.fromCharCode() 如何检查字符串是否包含JavaScript中的字符串数组中的子字符串 - how to check if a string contains a substring from an array of strings in JavaScript 检查字符串数组是否包含javascript中给定字符串的子字符串? - Check if an array of strings contains a substring of a given string in javascript? 确定字符串是否具有子字符串(单词) - Determining whether a string has a substring (word)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM