Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33164 > unrolled thread
| Started by | justaguy <lichunshen84@gmail.com> |
|---|---|
| First post | 2017-05-02 18:49 -0700 |
| Last post | 2017-05-19 07:58 -0700 |
| Articles | 17 — 5 participants |
Back to article view | Back to comp.lang.javascript
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
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2017-05-02 18:49 -0700 |
| Subject | How to perform an EXACT match for an item without looping through? |
| Message-ID | <5cc88464-96fb-45b6-b2ba-7b5e4f1d9a83@googlegroups.com> |
The following won't work. Is it doable without looping through the list?
Thanks.
<script>
/* find EXACT match of an item from a list without looping
in this case, the 2nd item */
var list = "2332,123456,9922";
var f = list.match(/12345/g);
if (f.length == 0) {
console.log('not found');
}
else {
console.log('found');
}
var list = "2332,123456,9922";
var f = list.indexOf('12345');
console.log(f);
</script>
[toc] | [next] | [standalone]
| From | JJ <jj4public@vfemail.net> |
|---|---|
| Date | 2017-05-03 18:36 +0700 |
| Message-ID | <1pmay28c1l3i8.1skar79uhfbmu.dlg@40tude.net> |
| In reply to | #33164 |
On Tue, 2 May 2017 18:49:20 -0700 (PDT), justaguy wrote:
> The following won't work. Is it doable without looping through the list?
>
> Thanks.
>
> <script>
> /* find EXACT match of an item from a list without looping
> in this case, the 2nd item */
>
> var list = "2332,123456,9922";
>
> var f = list.match(/12345/g);
>
> if (f.length == 0) {
> console.log('not found');
> }
> else {
> console.log('found');
> }
>
>
> var list = "2332,123456,9922";
>
> var f = list.indexOf('12345');
>
> console.log(f);
>
> </script>
The code does work. And it doesn't even use any loop already.
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2017-05-03 05:58 -0700 |
| Message-ID | <bf940060-7596-4738-82ae-fc7287be47de@googlegroups.com> |
| In reply to | #33177 |
On Wednesday, May 3, 2017 at 7:36:22 AM UTC-4, JJ wrote:
> On Tue, 2 May 2017 18:49:20 -0700 (PDT), justaguy wrote:
> > The following won't work. Is it doable without looping through the list?
> >
> > Thanks.
> >
> > <script>
> > /* find EXACT match of an item from a list without looping
> > in this case, the 2nd item */
> >
> > var list = "2332,123456,9922";
> >
> > var f = list.match(/12345/g);
> >
> > if (f.length == 0) {
> > console.log('not found');
> > }
> > else {
> > console.log('found');
> > }
> >
> >
> > var list = "2332,123456,9922";
> >
> > var f = list.indexOf('12345');
> >
> > console.log(f);
> >
> > </script>
>
> The code does work. And it doesn't even use any loop already.
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.
[toc] | [prev] | [next] | [standalone]
| From | JJ <jj4public@vfemail.net> |
|---|---|
| Date | 2017-05-04 15:03 +0700 |
| Message-ID | <dm6sbzq1icun$.1hh402ozq7wo3.dlg@40tude.net> |
| In reply to | #33179 |
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
As for regexp...
var f = list.match(/\b12345'b/g);
if (f.length == 0) {
console.log('not found');
}
else {
console.log('found');
}
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-04 10:57 +0200 |
| Message-ID | <XnsA76B6F722A82Ceejj99@194.109.6.166> |
| In reply to | #33188 |
JJ <jj4public@vfemail.net> wrote on 04 May 2017 in comp.lang.javascript:
> In that case...
>
> var list = "2332,123456,9922";
> As for regexp...
>
> var f = list.match(/\b12345'b/g);
That is what regex.test() is for.
> if (f.length == 0) {
> console.log('not found');
> }
> else {
> console.log('found');
> }
'use strict';
let list = '2332,123456,9922';
console.log(
( /,12345,/.test(','+list+',') )
? 'found' : 'not found'
); // not found
console.log(
( /,123456,/.test(','+list+',') )
? 'found' : 'not found'
); // found
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-05-05 18:40 +0200 |
| Message-ID | <2348668.BddDVKsqQX@PointedEars.de> |
| In reply to | #33188 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-05-05 19:59 +0200 |
| Message-ID | <3688698.1IzOArtZ34@PointedEars.de> |
| In reply to | #33179 |
Stefan Ram wrote:
> justaguy <lichunshen84@gmail.com> writes:
>>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.
>
> So, maybe, you're looking for
>
> f=(x,y)=>new RegExp(",([^,]*"+y+"[^,]*),").exec(","+x+",")[1];
> f("abcdefghi,jklmnopqrst,uvwxyz","mno")=="jklmnopqrst";
>
> then?
Good grief.
--
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.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-05-06 02:10 +0200 |
| Message-ID | <11781939.dW097sEU6C@PointedEars.de> |
| In reply to | #33224 |
Thomas 'PointedEars' Lahn wrote:
> Stefan Ram wrote:
>> justaguy <lichunshen84@gmail.com> writes:
>>>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.
>>
>> So, maybe, you're looking for
>>
>> f=(x,y)=>new RegExp(",([^,]*"+y+"[^,]*),").exec(","+x+",")[1];
^ ^^^^^^^ ^ ^^^^^ ^ ^^^^^^^^^^^^^^^
[1] [2] [3] [6] [4] [5][6]
>> f("abcdefghi,jklmnopqrst,uvwxyz","mno")=="jklmnopqrst";
>>
>> then?
>
> Good grief.
To elaborate, this is (from left to right)
· error-prone[^1]
· unnecessarily incompatible[^2]
· unnecessarily unreadable (hard to maintain)[^2][^4]
· insufficient[^3]
· unsafe[^4]
· unnecessarily complicated[^5]
· unnecessarily inefficient[^6]
IOW: Just. DO. NOT. Do. This. Ever.
--
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.
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2017-05-03 14:45 +0100 |
| Message-ID | <2onjgctpq57a8vqa6i0jp42t58ols8n9rt@4ax.com> |
| In reply to | #33164 |
On Tue, 2 May 2017 18:49:20 -0700 (PDT), justaguy
<lichunshen84@gmail.com> wrote:
>
>
>The following won't work. Is it doable without looping through the list?
>
>Thanks.
>
><script>
> /* find EXACT match of an item from a list without looping
> in this case, the 2nd item */
>
> var list = "2332,123456,9922";
>
> var f = list.match(/12345/g);
>
> if (f.length == 0) {
> console.log('not found');
> }
> else {
> console.log('found');
> }
>
>
> var list = "2332,123456,9922";
>
> var f = list.indexOf('12345');
>
> console.log(f);
>
></script>
Are you able to change the coding, e.g to
"|2332|123456|9922|" ?
Then looking for "|12345|" won't pick up "|123456|".
John
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2017-05-03 07:12 -0700 |
| Message-ID | <cfc2910b-c6c1-4da2-8166-42178b808527@googlegroups.com> |
| In reply to | #33181 |
On Wednesday, May 3, 2017 at 9:46:01 AM UTC-4, John G Harris wrote:
> On Tue, 2 May 2017 18:49:20 -0700 (PDT), justaguy
> <> wrote:
>
> >
> >
> >The following won't work. Is it doable without looping through the list?
> >
> >Thanks.
> >
> ><script>
> > /* find EXACT match of an item from a list without looping
> > in this case, the 2nd item */
> >
> > var list = "2332,123456,9922";
> >
> > var f = list.match(/12345/g);
> >
> > if (f.length == 0) {
> > console.log('not found');
> > }
> > else {
> > console.log('found');
> > }
> >
> >
> > var list = "2332,123456,9922";
> >
> > var f = list.indexOf('12345');
> >
> > console.log(f);
> >
> ></script>
>
> Are you able to change the coding, e.g to
> "|2332|123456|9922|" ?
> Then looking for "|12345|" won't pick up "|123456|".
>
> John
Nice. That works, John. Thanks.
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2017-05-17 09:58 -0700 |
| Message-ID | <28d725b0-3f1a-4af6-917f-1f8442dd08a6@googlegroups.com> |
| In reply to | #33181 |
On Wednesday, May 3, 2017 at 9:46:01 AM UTC-4, John G Harris wrote:
> On Tue, 2 May 2017 18:49:20 -0700 (PDT), justaguy
> wrote:
>
> >
> >
> >The following won't work. Is it doable without looping through the list?
> >
> >Thanks.
> >
> ><script>
> > /* find EXACT match of an item from a list without looping
> > in this case, the 2nd item */
> >
> > var list = "2332,123456,9922";
> >
> > var f = list.match(/12345/g);
> >
> > if (f.length == 0) {
> > console.log('not found');
> > }
> > else {
> > console.log('found');
> > }
> >
> >
> > var list = "2332,123456,9922";
> >
> > var f = list.indexOf('12345');
> >
> > console.log(f);
> >
> ></script>
>
> Are you able to change the coding, e.g to
> "|2332|123456|9922|" ?
> Then looking for "|12345|" won't pick up "|123456|".
>
> John
Hmm. There's a bug, that is, if a list item value is not strictly numeric data or if alphnumeric then they are also case sensitive otherwise such match technique fails. For instance, if we replace "12345" with "123AB45", then it won't match "123ab45", I'd like it to be case insensitive. Can we still use indexOf ? If so, how?
Thanks.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-17 19:34 +0200 |
| Message-ID | <XnsA778C7363C1D5eejj99@194.109.6.166> |
| In reply to | #33335 |
justaguy <lichunshen84@gmail.com> wrote on 17 May 2017 in
comp.lang.javascript:
> On Wednesday, May 3, 2017 at 9:46:01 AM UTC-4, John G Harris wrote:
>> On Tue, 2 May 2017 18:49:20 -0700 (PDT), justaguy
>> wrote:
>>
>> >
>> >
>> >The following won't work. Is it doable without looping through the
>> >list
> ?
>> >
>> >Thanks.
>> >
>> ><script>
>> > /* find EXACT match of an item from a list without looping
>> > in this case, the 2nd item */
>> >
>> > var list = "2332,123456,9922";
>> >
>> > var f = list.match(/12345/g);
>> >
>> > if (f.length == 0) {
>> > console.log('not found');
>> > }
>> > else {
>> > console.log('found');
>> > }
>> >
>> >
>> > var list = "2332,123456,9922";
>> >
>> > var f = list.indexOf('12345');
>> >
>> > console.log(f);
>> >
>> ></script>
>>
>> Are you able to change the coding, e.g to
>> "|2332|123456|9922|" ?
>> Then looking for "|12345|" won't pick up "|123456|".
>>
>> John
>
> Hmm. There's a bug, that is, if a list item value is not strictly
> numeric data or if alphnumeric then they are also case sensitive
> otherwise such match technique fails. For instance, if we replace
> "12345" with "123AB45", then it won't match "123ab45", I'd like it to be
> case insensitive. Can we still use indexOf ? If so, how?
Did not follow the thread,
but in general change the regex
var f = list.match(/123ab45/g);
to
var f = list.match(/123ab45/ig);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2017-05-17 11:12 -0700 |
| Message-ID | <bc377153-c2e8-4aa0-8a6d-318c3b65f6b1@googlegroups.com> |
| In reply to | #33336 |
On Wednesday, May 17, 2017 at 1:35:06 PM UTC-4, Evertjan. wrote:
> justaguy wrote on 17 May 2017 in
> comp.lang.javascript:
>
> > On Wednesday, May 3, 2017 at 9:46:01 AM UTC-4, John G Harris wrote:
> >> On Tue, 2 May 2017 18:49:20 -0700 (PDT), justaguy
> >> wrote:
> >>
> >> >
> >> >
> >> >The following won't work. Is it doable without looping through the
> >> >list
> > ?
> >> >
> >> >Thanks.
> >> >
> >> ><script>
> >> > /* find EXACT match of an item from a list without looping
> >> > in this case, the 2nd item */
> >> >
> >> > var list = "2332,123456,9922";
> >> >
> >> > var f = list.match(/12345/g);
> >> >
> >> > if (f.length == 0) {
> >> > console.log('not found');
> >> > }
> >> > else {
> >> > console.log('found');
> >> > }
> >> >
> >> >
> >> > var list = "2332,123456,9922";
> >> >
> >> > var f = list.indexOf('12345');
> >> >
> >> > console.log(f);
> >> >
> >> ></script>
> >>
> >> Are you able to change the coding, e.g to
> >> "|2332|123456|9922|" ?
> >> Then looking for "|12345|" won't pick up "|123456|".
> >>
> >> John
> >
> > Hmm. There's a bug, that is, if a list item value is not strictly
> > numeric data or if alphnumeric then they are also case sensitive
> > otherwise such match technique fails. For instance, if we replace
> > "12345" with "123AB45", then it won't match "123ab45", I'd like it to be
> > case insensitive. Can we still use indexOf ? If so, how?
>
> Did not follow the thread,
> but in general change the regex
>
> var f = list.match(/123ab45/g);
>
> to
>
> var f = list.match(/123ab45/ig);
>
Ok, I've made such change except I used
var e = document.getElementById('myID').value;
then set f = list.match(/e/ig);
And when e (element value is "123ab45"), returned f is still null, how come?
Thanks.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-17 20:33 +0200 |
| Message-ID | <XnsA778D11179439eejj99@194.109.6.166> |
| In reply to | #33337 |
justaguy <lichunshen84@gmail.com> wrote on 17 May 2017 in
comp.lang.javascript:
>> Did not follow the thread,
>> but in general change the regex
>>
>> var f = list.match(/123ab45/g);
>>
>> to
>>
>> var f = list.match(/123ab45/ig);
>>
>
> Ok, I've made such change except I used
> var e = document.getElementById('myID').value;
> then set f = list.match(/e/ig);
> And when e (element value is "123ab45"), returned f is still null, how
> come?
>
You cannot use a js-variable e in a regex string,
at least not this way:
var e = document.getElementById('myID').value;
var f = list.match(/e/ig);
better try:
var e = document.getElementById('myID').value;
var re = new RegExp( e,'ig' );
var f = list.match(re);
Regex is very powerfull, but it is not guesswork, read the specs.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2017-05-17 15:16 -0700 |
| Message-ID | <69e3d1ac-e112-436d-939a-e8736940927a@googlegroups.com> |
| In reply to | #33338 |
On Wednesday, May 17, 2017 at 2:33:13 PM UTC-4, Evertjan. wrote:
> justaguy wrote on 17 May 2017 in
> comp.lang.javascript:
>
> >> Did not follow the thread,
> >> but in general change the regex
> >>
> >> var f = list.match(/123ab45/g);
> >>
> >> to
> >>
> >> var f = list.match(/123ab45/ig);
> >>
> >
> > Ok, I've made such change except I used
> > var e = document.getElementById('myID').value;
> > then set f = list.match(/e/ig);
> > And when e (element value is "123ab45"), returned f is still null, how
> > come?
> >
>
> You cannot use a js-variable e in a regex string,
> at least not this way:
>
> var e = document.getElementById('myID').value;
> var f = list.match(/e/ig);
>
> better try:
>
> var e = document.getElementById('myID').value;
> var re = new RegExp( e,'ig' );
> var f = list.match(re);
>
It still fails. I've decided to loop through the values since it's a small loop anyway. Thanks tho.
> Regex is very powerfull, but it is not guesswork, read the specs.
>
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-05-18 11:15 +0200 |
| Message-ID | <XnsA779729B5E548eejj99@194.109.6.166> |
| In reply to | #33340 |
justaguy <lichunshen84@gmail.com> wrote on 18 May 2017 in
comp.lang.javascript:
>> var e = document.getElementById('myID').value;
>> var re = new RegExp( e,'ig' );
>> var f = list.match(re);
>>
>
> It still fails. I've decided to loop through the values since it's a
> small loop anyway. Thanks tho.
Using regex containing user-input will get you into trouble anyway,
since the user does not know about its special characters.
However a loop is NOT NEEDED, try String.search():
==============
'use strict';
let e, list = '2332,123AB45,9922';
e = '123ab45' // = document.getElementById('myID').value;
found(e, list); // true
e = '123ab4' // = document.getElementById('myID').value;
found(e, list); // false
function found(e, list) {
list = ',' + list.toLowerCase() + ',';
e = ',' + e.toLowerCase() + ',';
let f = list.search(e) != -1;
document.write(f);
};
==============
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2017-05-19 07:58 -0700 |
| Message-ID | <f2cc6d1a-5042-467e-b450-267edf038d18@googlegroups.com> |
| In reply to | #33341 |
On Thursday, May 18, 2017 at 5:16:04 AM UTC-4, Evertjan. wrote:
> justaguy wrote on 18 May 2017 in
> comp.lang.javascript:
>
> >> var e = document.getElementById('myID').value;
> >> var re = new RegExp( e,'ig' );
> >> var f = list.match(re);
> >>
> >
> > It still fails. I've decided to loop through the values since it's a
> > small loop anyway. Thanks tho.
>
> Using regex containing user-input will get you into trouble anyway,
> since the user does not know about its special characters.
>
> However a loop is NOT NEEDED, try String.search():
>
> ==============
> 'use strict';
> let e, list = '2332,123AB45,9922';
>
> e = '123ab45' // = document.getElementById('myID').value;
> found(e, list); // true
>
> e = '123ab4' // = document.getElementById('myID').value;
> found(e, list); // false
>
>
> function found(e, list) {
> list = ',' + list.toLowerCase() + ',';
> e = ',' + e.toLowerCase() + ',';
> let f = list.search(e) != -1;
> document.write(f);
> };
Interesting and good to know about the JS's "search" function. In my code for the loop, I also turned element value and the other side of the comparison equation into uppercase to make match case insensitive. Thanks.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web