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)
这篇关于如何检查字符串是否在不同位置包含多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检查字符串是否在不同位置包含多个单词
基础教程推荐
- 我什么时候应该在导入时使用方括号 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 动态更新多个选择框 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01