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

How to check if a string contains multiple words on different locations(如何检查字符串是否在不同位置包含多个单词)

本文介绍了如何检查字符串是否在不同位置包含多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有多个这样的字符串:

So I have multiple string that are like this:

字符串 1:那里这是我正在尝试做的一件事,但我不知道如何做.
字符串 2:我真的不知道如何修复它.
字符串 2:希望有人可以帮助我.

String 1: There is this one thing that I'm trying to do but I have no idea how.
String 2: I really have no clue how too fix it.
String 2: Hopefully maybe someone can help me.

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

Now I also have a string that is a search input that can be anything, for example:

有想法

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

When the user inputs and sends that I want the JavaScript too match with string 1.
Also I would like too have it return by how many characters the two strings are matching.

如果有人可以帮助我,我将非常感激.

If someone can help me with this I would be very grateful.

提前致谢,

工作

推荐答案

如果想知道匹配的长度,例如使用正则表达式:

If you want to know the length of the match, e.g. using 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 )
  

 匹配 字边界(零-长度匹配)

 matches a word boundary (zero-length match)

这篇关于如何检查字符串是否在不同位置包含多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何检查字符串是否在不同位置包含多个单词

基础教程推荐