如何检查字符串是否在不同位置包含多个单词

How to check if a string contains multiple words on different locations

本文关键字:位置 包含多 单词 是否 何检查 检查 字符串      更新时间:2023-09-26

那么我就有了这样的多个字符串:

String 1: 那里是我想做的一件事,但我没有想法如何。
字符串2:我真的不知道该怎么修复它。字符串2:希望有人能帮我。

现在我也有一个字符串,它是一个搜索输入,可以是任何东西,例如:

有想法

当用户输入并发送时,我希望JavaScript也与字符串1匹配。
我也想让它返回多少字符两个字符串匹配。

如果有人能帮我,我将非常感激。

提前感谢,

工作

如果你想知道匹配的长度,例如使用regex:

var str = "There idea";
var pattern = new RegExp("''b" + str.replace(/ +/g, "''b.*''b") + "''b", "i")
console.log(pattern)
var strings = [
  "There is this one thing that I'm trying to do but I have no idea how",
  "I really have no clue how too fix it",
  "Hopefully maybe someone can help me"
]
for( i=0; i < strings.length; i++ ) 
  if( res=strings[i].match(pattern) ) 
    console.log( res[0], res[0].length )
  

'b匹配一个词边界(零长度匹配)

<script>
        $(document).ready(function () {
            var myTestValue = ['test1', 'test2', 'test3'];
            for (var i = 0; i < myTestValue.length; i++) {
                if(window.location.href.indexOf(myTestValue[i]) > -1) {
                    alert("your contains the string "+myTestValue[i]);
                }
            }
        });
</script>
我不知道这对你有没有帮助。但是我希望你能从@Job storm中得到灵感