Reputation: 814
This answer helps a lot when we have one word in string and one word in substring. Method includes()
do the job. Any idea how to solve this if there is more than one word in string and more then one word in substring? For example:
const string = "hello world";
const substring = "hel wo";
console.log(string.includes(substring)); --> //This is false, but I want to change the code to return true
Should I create some arrays from string and substring using split(" ")
and than somehow compare two arrays?
Upvotes: 0
Views: 30
Reputation: 184
you can use regex for this:
var str = "hello world";
var patt = new RegExp(/.{0,}((?:hel)).{0,}((?:wo)).{0,}/ig);
console.log(patt.test(str));
Upvotes: 0
Reputation: 6828
You can split the "substring" on whitespace to get an array of all substrings you want to look for and then use Array.prototype.every()
to see if all substrings are found:
const string = "hello world";
const substring = "hel wo";
console.log(substring.split(/\s+/).every(substr => string.includes(substr)));
Upvotes: 2