简体   繁体   English

Javascript - 如何在一个条件下检查字符串是否包含多个子字符串

[英]Javascript - How to check if a string contains multiple substrings in one condition

I want to create an if condition that check if the string contains two keywords for example i have this str: initcall7773107b-7273-464d-9374-1bff75accc15TopCenter how check if this str contains: initcall && TopCenter in addition there is another string must added to condition so the scenario will be like this if(first_str.includes('initcall','TopCenter') && second_str.includes('start', 'BottomLeft') { // Do somthig })我想创建一个 if 条件来检查字符串是否包含两个关键字,例如我有这个 str: initcall7773107b-7273-464d-9374-1bff75accc15TopCenter如何检查这个 str 是否包含: initcall && TopCenter另外还有另一个字符串必须添加到条件所以场景将是这样的if(first_str.includes('initcall','TopCenter') && second_str.includes('start', 'BottomLeft') { // Do somthig })

after searching i found that how to check if one keyword includes in the string not two or using regExp, so i need to check for two words in one string and add another parameter to the condition as mentioned above.搜索后我发现如何检查一个关键字是否包含在字符串中而不是两个或使用 regExp,所以我需要检查一个字符串中的两个单词并在条件中添加另一个参数,如上所述。

This statement sub1.map(string => str.includes(string)).every(Boolean) is basically taking every string from the sub1 array and checking if it is included in the original string str creating an array of booleans, the.every() evaluates the array of booleans by calling the boolean function that evaluates the whole statement to a single boolean.这个语句sub1.map(string => str.includes(string)).every(Boolean)基本上是从 sub1 数组中获取每个字符串并检查它是否包含在原始字符串 str 中,创建一个布尔数组 the.every () 通过调用 boolean function 来评估布尔数组,它将整个语句评估为单个 boolean。

  var str = "initcall7773107b-7273-464d-9374-1bff75accc15TopCenter";
  var sub1 = ["initcall","TopCenter"];
  var sub2 = ["start","BottomLeft"]

  var n = sub1.map(string => str.includes(string)).every(Boolean) && sub2.map(string => str.includes(string)).every(Boolean);

  console.log(n);

reference to .every() function & Boolean function参考.every() function & Boolean function

I can only think of this:我只能这么想:

 'use strict'; String.prototype.includes = function (...args) { return args.filter(str => this.indexOf(str) > -1).length === args.length; }; var str = 'initcall7773107b-7273-464d-9374-1bff75accc15TopCenter'; if(str.includes('initcall', 'TopCenter')) { console.log('Do something...'); }

.every() will stop processing immediately if the method passed to it returns false .如果传递给它的方法返回false.every()将立即停止处理。 The above suggestion does the check in the method passed to .map() , which runs on every element unconditionally.上述建议检查传递给.map()的方法,该方法无条件地在每个元素上运行。 Why not use just .every() so that negative results are found faster?为什么不只使用.every()以便更快地找到负面结果?

Here's some injection that will demonstrate:这是一些将演示的注入:

let counter;
const includesAllWithMap = (s, ...args) => args.map((v) => {
  counter += 1;
  return s.includes(v);
}).every(Boolean);

const includesAllWithJustEvery = (s, ...args) => args.every((v) => {
  counter += 1;
  return s.includes(v);
});

counter = 0;
rc = includesAllWithMap('abcdef', 'ab', 'cd', 'ba', 'ef', 'a', 'c');
console.log('result with map =', rc, 'number of comparisons =', counter);
// result with map = false number of comparisons = 6

counter = 0;
rc = includesAllWithJustEvery('abcdef', 'ab', 'cd', 'ba', 'ef', 'a', 'c');
console.log('result with just every =', rc, 'number of comparisons =', counter);
// result with map = false number of comparisons = 3

To make it succinct:为了简洁:

const includesAll = (s, ...args) => args.every(v => s.includes(v));
> includesAll('abcdef', 'ab', 'cd', 'ba', 'ef', 'a', 'c')
// false
> includesAll('abcd', 'a', 'd')
// true

You can make a slight change if you want to provide the substrings in arrays, as the original poster wanted:如果您想在 arrays 中提供子字符串,您可以稍作更改,正如原始海报所希望的那样:

const includesAll = (s, ...args) => args.flat().every(v => s.includes(v));
> includesAll('abcdef', ['ab', 'cd', 'ba'], ['ef', 'a', 'c'])
// false
> includesAll('abcd', ['a', 'd'])
// true
> includesAll('abcdef', ['ab', 'abc'], ['de'], ['bcd'])
// true

And if you want both:如果你想要两者:

const includesAll = (s, ...args) => args.every(v => s.includes(v));
const includesAllinArrays = (s, ...args) => includeAll(s, args.flat());

And if you want an all-in-one (at the sacrifice of some efficiency):如果你想要一个一体机(牺牲一些效率):

const includesAll = (s, ...args) => args
  .map(a => Array.isArray(a) ? a.flat() : [a])
  .flat()
  .every(v => s.includes(v));
> includesAll('abcdef', 'ab', 'abc', ['d'], ['d', 'e'], 'cd')
// true

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

相关问题 Javascript - 如何检查一个字符串是否包含多个子字符串 - Javascript - How to check if a string contains multiple substrings 如何检查字符串是否包含 JavaScript 中子字符串数组中的文本? - How to check if a string contains text from an array of substrings in JavaScript? 如何检查 URL 字符串是否包含 2 个子字符串? - How to check if a URL string contains either of 2 substrings? jquery/javascript 检查多个子字符串的字符串 - jquery/javascript check string for multiple substrings 如何在JavaScript中多个子串中的一个子串上拆分字符串? - How to split a string on the first occurrence of one of multiple substrings in JavaScript? Javascript在一个字符串中获取多个子字符串 - Javascript Get Multiple Substrings Within One String 如何在JavaScript中检查一个字符串是否包含变量值? - How can I check if one string contains a variable value in JavaScript? JavaScript 如何检查字符串是否至少包含一个字母? - How to check if a string contains at least one letter in JavaScript? 如何检查字符串是否包含 Javascript 中数组中的元素之一? - How to check if a string contains one of the elements from an array in Javascript? 检查字符串是否包含所有子字符串的最快方法 - Fastest way to check if string contains all substrings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM