Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33222
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: How to perform an EXACT match for an item without looping through? |
| Date | 2017-05-05 18:40 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <2348668.BddDVKsqQX@PointedEars.de> (permalink) |
| References | <5cc88464-96fb-45b6-b2ba-7b5e4f1d9a83@googlegroups.com> <1pmay28c1l3i8.1skar79uhfbmu.dlg@40tude.net> <bf940060-7596-4738-82ae-fc7287be47de@googlegroups.com> <dm6sbzq1icun$.1hh402ozq7wo3.dlg@40tude.net> |
JJ wrote:
> 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: <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
How to perform an EXACT match for an item without looping through? justaguy <lichunshen84@gmail.com> - 2017-05-02 18:49 -0700
Re: How to perform an EXACT match for an item without looping through? JJ <jj4public@vfemail.net> - 2017-05-03 18:36 +0700
Re: How to perform an EXACT match for an item without looping through? justaguy <lichunshen84@gmail.com> - 2017-05-03 05:58 -0700
Re: How to perform an EXACT match for an item without looping through? JJ <jj4public@vfemail.net> - 2017-05-04 15:03 +0700
Re: How to perform an EXACT match for an item without looping through? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-04 10:57 +0200
Re: How to perform an EXACT match for an item without looping through? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:40 +0200
Re: How to perform an EXACT match for an item without looping through? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 19:59 +0200
Re: How to perform an EXACT match for an item without looping through? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 02:10 +0200
Re: How to perform an EXACT match for an item without looping through? John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-03 14:45 +0100
Re: How to perform an EXACT match for an item without looping through? justaguy <lichunshen84@gmail.com> - 2017-05-03 07:12 -0700
Re: How to perform an EXACT match for an item without looping through? justaguy <lichunshen84@gmail.com> - 2017-05-17 09:58 -0700
Re: How to perform an EXACT match for an item without looping through? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-17 19:34 +0200
Re: How to perform an EXACT match for an item without looping through? justaguy <lichunshen84@gmail.com> - 2017-05-17 11:12 -0700
Re: How to perform an EXACT match for an item without looping through? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-17 20:33 +0200
Re: How to perform an EXACT match for an item without looping through? justaguy <lichunshen84@gmail.com> - 2017-05-17 15:16 -0700
Re: How to perform an EXACT match for an item without looping through? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-18 11:15 +0200
Re: How to perform an EXACT match for an item without looping through? justaguy <lichunshen84@gmail.com> - 2017-05-19 07:58 -0700
csiph-web