let theString = 'include all the information someone would need to answer your question'
let theString2 = 'include some the information someone would need to answer your question'
let theString3 = 'the information someone would need to answer your question'
let theArray = ['all', 'some', 'x', 'y', 'etc' ]
theString true because there is 'all', theString2 true because there is 'some', theString3 false because there is no 'all' or 'some'
const theStringIsGood = theString.split(' ').some(word => theArray.includes(word))
此代码将句子拆分为每个单词,并检查是否有任何单词与匹配集 theArray 匹配。
You could use the string.includes() method. But in this case theString3 would still return true because some is in something. You could create a helper function to see if a string is within an array instead:
function findString(arr, str) {
let flag = false;
let strArr = str.split(' ');
arr.forEach(function(s) {
strArr.forEach(function(s2) {
if(s === s2) {
flag = true;
}
});
});
return flag;
};
Now you can pass the array and one of the strings to the function and test it.
you can do this with regex simply :
function ArrayInText(str, words) {
let regex = new RegExp("\\b(" + words.join('|') + ")\\b", "g");
return regex.test(str);
}
in regex \\b is words boundary
and notice, if you split all string yourself and check one by one, a lot of memory use, because it is a greedy solution. I offer that using javascript native functions.
snippet :
let theString = 'include all the information someone would need to answer your question' let theString2 = 'include some the information someone would need to answer your question' let theString3 = 'the information someone would need to answer your question' let theArray = ['all', 'some', 'x', 'y', 'etc'] function ArrayInText(str, words) { let regex = new RegExp("\\\\b(" + words.join('|') + ")\\\\b", "g"); return regex.test(str); } console.log(ArrayInText(theString, theArray)); console.log(ArrayInText(theString2, theArray)); console.log(ArrayInText(theString3, theArray));
good luck :)
1) isIncludeWord
-- look for exact word match using split
and includes
method.
2) isInclude
-- look for match contain using some
and includes
.
let theString = "include all the information someone would need to answer your question"; let theString2 = "include some the information someone would need to answer your question"; let theString3 = "the information someone would need to answer your question"; let theArray = ["all", "some", "x", "y", "etc"]; const isIncludeWord = (str, arr) => str.split(" ").reduce((include, word) => include || arr.includes(word), false); const isInclude = (str, arr) => arr.some(item => str.includes(item)); console.log(isIncludeWord(theString, theArray)); console.log(isIncludeWord(theString2, theArray)); console.log(isIncludeWord(theString3, theArray)); console.log(isInclude(theString, theArray)); console.log(isInclude(theString2, theArray)); console.log(isInclude(theString3, theArray));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.