Path: csiph.com!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.191.227.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Find part match in array Date: Mon, 17 Aug 2020 19:36:50 +0200 Organization: PointedEars Software (PES) Lines: 51 Message-ID: <1927146.oMNUckLgyt@PointedEars.de> References: <9rCdnYySpppZSKTCnZ2dnUU7-N3NnZ2d@westnet.com.au> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="191.227.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch:178.197.227.191"; logging-data="22582"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Lock: sha1:9V195r2BkGgi4CXdqmog5g+D3As= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-User-ID: U2FsdGVkX18PipPTGEM2JBgyolo4quSc7Pd3r98J0FXtFb7ooHzVoQ== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. If I have this > > let arr1 = ["Today", "Freddy", "was", "here"]; > > what's a straightforward way to find that there's a match with this array > arr2 = ["Fred", "Yesterday"]; > (the first element "matches" the second element in arr1 ie 'Fred' is > within 'Freddy')? > > I started with > const matched = arr1.some(a => arr2.includes(a)); > but I'm having trouble replacing the "includes" with a regexp and it to > not error. To arrive at a solution, it helps to think about the problem figuratively and to give your variables more meaningful names. It also makes your code easier to understand later: let haystack = ["Today", "Freddy", "was", "here"]; let needles = ["Fred", "Yesterday"]; /* true if any of the needles was found in the haystack */ const found = needles.some(needle => { /* Return true if the needle was found inside a straw of the haystack */ return haystack.some(straw => (new RegExp(needle)).test(straw)); }); Notice that there is a difference as to whether you match the contents of the second array against the contents of the first one or vice-versa. Also notice that 1. You do not have to use regular expressions. Especially in ECMAScript Ed. 6 and newer, there are methods like String.prototype.includes() which with you can search for substrings easily. 2. If you use regular expressions, the needle should be escaped if you want to search for a string verbatim instead of matching a pattern against an element: … new RegExp(needle.replace(/[\]\\^$*+?.(){}|[]/g, "\\$&")) … See also p. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.