Path: csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: How to perform an EXACT match for an item without looping through? Date: Fri, 05 May 2017 18:40:08 +0200 Organization: PointedEars Software (PES) Lines: 82 Message-ID: <2348668.BddDVKsqQX@PointedEars.de> References: <5cc88464-96fb-45b6-b2ba-7b5e4f1d9a83@googlegroups.com> <1pmay28c1l3i8.1skar79uhfbmu.dlg@40tude.net> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1494002409 27339 eJwFwYkBwCAIA8CVSAmP4yia/UfoXXgip5iRDIVMmls6b90GD+UwxG2b2h0g1jub1sttcz78Km0Q9A== (5 May 2017 16:40:09 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Fri, 5 May 2017 16:40:09 +0000 (UTC) User-Agent: KNode/4.14.2 X-NNTP-Posting-Host: eJwFwYEBgDAIA7CXCqMVz0EG/59gwiNTPyEquNwYluEi5IhOLXvOq158abkDGiaDVfcm3H8SHxDC X-User-ID: eJwFwQcBACAMAzBL7HRHDuz4l0ACMbJyNZhiseV9uIkV80LwdOHZ4Jq8W9UkudSsczzNnWvbpiVenNCS+wFdGxXR Cancel-Lock: sha1:x5IzWDq3YJtL4cJWK8ZJskjphV0= 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-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. On Wed, 3 May 2017 05:58:25 -0700 (PDT), justaguy wrote: >> Sorry, it must be a misunderstanding. For instance, >> list.indexOf('12345') produces "found", which is wrong because it's not >> the same as '123456', the latter is an item from the list while the >> '12345' is not. > > In that case... > > var list = "2332,123456,9922"; > > var f = list.split(",").indexOf("12345"); > console.log(f); //-1 > var f = list.split(",").indexOf("123456"); > console.log(f); //1 That would have been my suggestion, too. (With the exception of the identifier, see below.) > As for regexp... > > var f = list.match(/\b12345'b/g); ^ var f = list.match(/\b12345\b/g); > if (f.length == 0) { String.prototype.match() returns “null” if there is no match, therefore this code will throw the exception “null has no properties” in that case. The proper test is if (f === null) Or simpler: if (f == null) Or even simpler: if (!f) > console.log('not found'); > } > else { > console.log('found'); > } And since double negation should be avoided in favor of clarity, if (f) { console.log("found"); } else { console.log("not found"); } “f” is not a wise identifier for potential matches. If the matches are not used, String.prototype.match() is the wrong method to call, and the “g”(lobal) flag is superfluous. Instead, call RegExp.prototype.test(): if (/\b12345\b/.test(list)) { console.log("found"); } else { console.log("not found"); } Note that if "the needle" is not fixed, RegExp-special characters might need to be escaped before passing them to the RegExp(-like) constructor. -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.