Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #38734
| 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 <PointedEars@web.de> |
| 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> (permalink) |
| References | <9rCdnYySpppZSKTCnZ2dnUU7-N3NnZ2d@westnet.com.au> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| 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+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Xref | csiph.com comp.lang.javascript:38734 |
Show key headers only | View raw
Andrew Poulos wrote:
> 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
<https://github.com/PointedEars/JSX/blob/master/regexp.js#L1441> p.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-17 10:56 +1000
Re: Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-17 13:06 +1000
Re: Find part match in array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-08-16 20:54 -0700
Re: Find part match in array JJ <jj4public@vfemail.net> - 2020-08-17 16:36 +0700
Re: Find part match in array Julio Di Egidio <julio@diegidio.name> - 2020-08-17 05:01 -0700
Re: Find part match in array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-17 19:36 +0200
Re: Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-18 09:02 +1000
Re: Find part match in array Andrew Poulos <ap_prog@hotmail.com> - 2020-08-18 13:09 +1000
Re: Find part match in array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-18 11:28 +0200
Re: Find part match in array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2020-08-18 12:59 +0200
Re: Find part match in array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-08-18 21:59 +0200
csiph-web