Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18955 > unrolled thread

Help creating regex

Started by"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
First post2013-03-20 11:47 -0700
Last post2013-04-29 01:40 -0700
Articles 20 on this page of 22 — 7 participants

Back to article view | Back to comp.lang.javascript


Contents

  Help creating regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-03-20 11:47 -0700
    Re: Help creating regex Danny <dann90038@gmail.com> - 2013-03-20 12:08 -0700
      Re: Help creating regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-03-20 12:18 -0700
    Re: Help creating regex Jim T. <x@y.z> - 2013-03-20 15:11 -0500
      Re: Help creating regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-03-20 12:19 -0700
        Re: Help creating regex Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-21 20:20 +0000
    Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-27 10:56 -0700
      Re: Help creating 'simple' regex JJ <duh@nah.meh> - 2013-04-28 02:27 +0700
        Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-27 16:39 -0700
        Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-28 09:36 -0700
          Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-29 01:41 -0700
      Re: Help creating 'simple' regex Stefan Weiss <krewecherl@gmail.com> - 2013-04-27 22:27 +0200
        Re: Help creating 'simple' regex Stefan Weiss <krewecherl@gmail.com> - 2013-04-27 22:34 +0200
        Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-27 16:39 -0700
          Re: Help creating 'simple' regex "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-04-28 09:20 +0200
            Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-28 01:22 -0700
              Re: Help creating 'simple' regex Stefan Weiss <krewecherl@gmail.com> - 2013-04-28 11:32 +0200
                Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-28 09:53 -0700
                  Re: Help creating 'simple' regex Stefan Weiss <krewecherl@gmail.com> - 2013-04-28 19:41 +0200
                    Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-28 23:51 -0700
              Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-28 09:14 -0700
              Re: Help creating 'simple' regex "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> - 2013-04-29 01:40 -0700

Page 1 of 2  [1] 2  Next page →


#18955 — Help creating regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-03-20 11:47 -0700
SubjectHelp creating regex
Message-ID<mq0kk8ljpnig2uhebno0ogrb2tc2gqb4oo@4ax.com>
I know next to nothing about regex, but I need to determine whether a
decimal value is represented in a string.

Specifically, if the string 'XX' contains the phrase 7.4 within XX, I need
a test to come out true. 

I've gone batty trying to do this, and I continually fail. Can someone
help?

Thanks in advance,
-- 
Joey

[toc] | [next] | [standalone]


#18956

FromDanny <dann90038@gmail.com>
Date2013-03-20 12:08 -0700
Message-ID<3acc198e-9d81-4fbc-af53-6a593007ba6a@googlegroups.com>
In reply to#18955
To Joey

Give us the string example you're working with and the query you want, more specifically.  To just get 2 numbers with a dot something like /\d\.\d/.test(YOURSTRINGHERE); will do.

[toc] | [prev] | [next] | [standalone]


#18958

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-03-20 12:18 -0700
Message-ID<er2kk8140ceialcpu5htmuk4lagpq5ljli@4ax.com>
In reply to#18956
Danny wrote:

>To Joey
>
>Give us the string example you're working with and the query you want, more specifically.  To just get 2 numbers with a dot something like /\d\.\d/.test(YOURSTRINGHERE); will do.

Thanks.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#18957

FromJim T. <x@y.z>
Date2013-03-20 15:11 -0500
Message-ID<sq5kk85bg1uk5vkg0brt10hhk5a8cjh3ac@4ax.com>
In reply to#18955
On Wed, 20 Mar 2013 11:47:09 -0700, "Joey@still_Learning.invalid"
<Joey@still_Learning.invalid> wrote:

>I know next to nothing about regex, but I need to determine whether a
>decimal value is represented in a string.
>
>Specifically, if the string 'XX' contains the phrase 7.4 within XX, I need
>a test to come out true. 

Unless I misunderstood your question you don't need RegExp, indexOf()
will do.

[toc] | [prev] | [next] | [standalone]


#18959

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-03-20 12:19 -0700
Message-ID<vr2kk85pj3kfai9b9mu2qpuom24deooiol@4ax.com>
In reply to#18957
Jim T. wrote:

>On Wed, 20 Mar 2013 11:47:09 -0700, "Joey@still_Learning.invalid"
><Joey@still_Learning.invalid> wrote:
>
>>I know next to nothing about regex, but I need to determine whether a
>>decimal value is represented in a string.
>>
>>Specifically, if the string 'XX' contains the phrase 7.4 within XX, I need
>>a test to come out true. 
>
>Unless I misunderstood your question you don't need RegExp, indexOf()
>will do.

You understood it correctly, and I resolved my issue using indexOf.
Thanks.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#18963

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-03-21 20:20 +0000
Message-ID<E39XW5DPu2SRFwMU@invalid.uk.co.demon.merlyn.invalid>
In reply to#18959
In comp.lang.javascript message <vr2kk85pj3kfai9b9mu2qpuom24deooiol@4ax.
com>, Wed, 20 Mar 2013 12:19:07, "Joey@still_Learning.invalid"
<Joey@still_Learning.invalid> posted:

>Jim T. wrote:
>
>>On Wed, 20 Mar 2013 11:47:09 -0700, "Joey@still_Learning.invalid"
>><Joey@still_Learning.invalid> wrote:
>>
>>>I know next to nothing about regex, but I need to determine whether a
>>>decimal value is represented in a string.
>>>
>>>Specifically, if the string 'XX' contains the phrase 7.4 within XX, I need
>>>a test to come out true.
>>
>>Unless I misunderstood your question you don't need RegExp, indexOf()
>>will do.
>
>You understood it correctly, and I resolved my issue using indexOf.

But there is, for most purposes, no decimal value 7.4 in the string
"aaa67.45zzz"; and, depending on circumstances, there is no decimal
value 7.4 in the string "7.4e5", although there is one in "7.4e0".

An ill-considered or inexact question will often, but not necessarily,
get an answer inappropriate for the purpose.

-- 
(c) John Stockton, nr London UK.  ???@merlyn.demon.co.uk  Turnpike v6.05  MIME.
 Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
     Check boilerplate spelling -- error is a public sign of incompetence.
    Never fully trust an article from a poster who gives no full real name.

[toc] | [prev] | [next] | [standalone]


#19503 — Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-27 10:56 -0700
SubjectHelp creating 'simple' regex
Message-ID<gt3on8tkhr5iu14im8cprl1jtre1e4pm7u@4ax.com>
In reply to#18955
I know next to nothing about regex, but I need to determine whether an
exact match of a string within a string exists. My current code, which
doesn't work is:

firstString = 'testing';
testString = 'This is useful for testing cookies';

testTheStrings = testString.search(/\bfirstString\b/);
	
if (testTheStrings !=-1) {alert("Names are OK")}
else {alert("Names not the same");}

Keeps coming up with testTheStrings = -1.

What am I doing wrong?

TIA,
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#19504 — Re: Help creating 'simple' regex

FromJJ <duh@nah.meh>
Date2013-04-28 02:27 +0700
SubjectRe: Help creating 'simple' regex
Message-ID<1culsl58861ny.zc5g3yg3m79c$.dlg@40tude.net>
In reply to#19503
On Sat, 27 Apr 2013 10:56:20 -0700, Joey@still_Learning.invalid wrote:
> I know next to nothing about regex, but I need to determine whether an
> exact match of a string within a string exists. My current code, which
> doesn't work is:
> 
> firstString = 'testing';
> testString = 'This is useful for testing cookies';
> 
> testTheStrings = testString.search(/\bfirstString\b/);
> 	
> if (testTheStrings !=-1) {alert("Names are OK")}
> else {alert("Names not the same");}
> 
> Keeps coming up with testTheStrings = -1.
> 
> What am I doing wrong?

What you did there is to search for "firstString" instead of "testing". So
the regex should be:

/\btesting\b/

i.e.: the text itself. not the variable name of the text you want to search.

So te code should be like this:

testTheStrings = testString.search(/\btesting\b/);

If you want to use a variable to pass to the search function...

firstString = /\btesting\b/;
testString = 'This is useful for testing cookies';
testTheStrings = testString.search(firstString);

For case-insensitive regex, use the "i" option:

firstString = /\btesting\b/i;

[toc] | [prev] | [next] | [standalone]


#19510 — Re: Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-27 16:39 -0700
SubjectRe: Help creating 'simple' regex
Message-ID<8doon8d9i9q8i50fkd0fk4acbui3k19ips@4ax.com>
In reply to#19504
JJ wrote:

>On Sat, 27 Apr 2013 10:56:20 -0700, Joey@still_Learning.invalid wrote:
>> I know next to nothing about regex, but I need to determine whether an
>> exact match of a string within a string exists. My current code, which
>> doesn't work is:
>> 
>> firstString = 'testing';
>> testString = 'This is useful for testing cookies';
>> 
>> testTheStrings = testString.search(/\bfirstString\b/);
>> 	
>> if (testTheStrings !=-1) {alert("Names are OK")}
>> else {alert("Names not the same");}
>> 
>> Keeps coming up with testTheStrings = -1.
>> 
>> What am I doing wrong?
>
>What you did there is to search for "firstString" instead of "testing". So
>the regex should be:
>
>/\btesting\b/
>
>i.e.: the text itself. not the variable name of the text you want to search.
>
>So te code should be like this:
>
>testTheStrings = testString.search(/\btesting\b/);
>
>If you want to use a variable to pass to the search function...
>
>firstString = /\btesting\b/;
>testString = 'This is useful for testing cookies';
>testTheStrings = testString.search(firstString);
>
>For case-insensitive regex, use the "i" option:
>
>firstString = /\btesting\b/i;

Thanks very much, JJ. 
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#19519 — Re: Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-28 09:36 -0700
SubjectRe: Help creating 'simple' regex
Message-ID<rmiqn8dniv7fj2heal49qae2s21vrpe66s@4ax.com>
In reply to#19504
JJ wrote:

>On Sat, 27 Apr 2013 10:56:20 -0700, Joey@still_Learning.invalid wrote:
>> I know next to nothing about regex, but I need to determine whether an
>> exact match of a string within a string exists. My current code, which
>> doesn't work is:
>> 
>> firstString = 'testing';
>> testString = 'This is useful for testing cookies';
>> 
>> testTheStrings = testString.search(/\bfirstString\b/);
>> 	
>> if (testTheStrings !=-1) {alert("Names are OK")}
>> else {alert("Names not the same");}
>> 
>> Keeps coming up with testTheStrings = -1.
>> 
>> What am I doing wrong?
>
>What you did there is to search for "firstString" instead of "testing". So
>the regex should be:
>
>/\btesting\b/
>
>i.e.: the text itself. not the variable name of the text you want to search.
>
>So te code should be like this:
>
>testTheStrings = testString.search(/\btesting\b/);
>
>If you want to use a variable to pass to the search function...
>
>firstString = /\btesting\b/;
>testString = 'This is useful for testing cookies';
>testTheStrings = testString.search(firstString);
>
>For case-insensitive regex, use the "i" option:
>
>firstString = /\btesting\b/i;

Thanks, again for this JJ. While it works as you have shown, it isn't
solving my dilemma. I need to amplify my issue...

Users enter 'firstString' into a form element. They later enter
'testString' into another form element. My task is to assure that
firstString is exactly contained within testString. 

firstString = document.form.elements["firstString"].value;
testString = document.form.elements["testString"].value;

I tried your suggestion, i.e.,

firstString2 =  /\bfirstString\b/;
testTheStrings = testString.search(firstString2);

But, as I'm sure you already know, it won't play.

Thanks again for your help.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#19528 — Re: Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-29 01:41 -0700
SubjectRe: Help creating 'simple' regex
Message-ID<4gcsn8p1pab54kpc2sr1s91ccm4a4m9udh@4ax.com>
In reply to#19519
Joey@still_Learning.invalid wrote:

>JJ wrote:
>
>Thanks, again for this JJ. While it works as you have shown, it isn't
>solving my dilemma. I need to amplify my issue...
>
Problem solved...just took some prodding to think. Thanks for your help.

firstString = document.form.elements["firstString"].value; //'Mary Smith'
testString = document.form.elements["testString"].value;
//Mary Smith-00003.img

adjName = testString.indexOf("-00");
endName = imageName0.substr(0,adjName);
testNames = endName.indexOf(firstString);
if (testNames != -1 && firstString == endName)  {
	alert("Identical")
}
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#19505 — Re: Help creating 'simple' regex

FromStefan Weiss <krewecherl@gmail.com>
Date2013-04-27 22:27 +0200
SubjectRe: Help creating 'simple' regex
Message-ID<klhcb9$vdr$1@news.albasani.net>
In reply to#19503
On 2013-04-27 19:56, Joey@still_Learning.invalid wrote:
> I know next to nothing about regex, but I need to determine whether an
> exact match of a string within a string exists. My current code, which
> doesn't work is:
> 
> firstString = 'testing';
> testString = 'This is useful for testing cookies';
> 
> testTheStrings = testString.search(/\bfirstString\b/);

In addition to what JJ wrote: if you're looking for an exact match and
don't really need the word boundary matches, you could also use indexOf():

  var found = testString.indexOf("testing") > -1;

indexOf() can be a lot faster than a regular expression, depending on
the implementation. This example would also match "protesting" and
"testings". You can avoid that by searching for " testing ", if you know
that the word will be surrounded by spaces.

If you do need the regex, but are only interested in a true/false result
of the match, use the test() method instead of search():

  var found = /\btesting\b/.test(testString);

It returns a boolean and is faster and arguably more expressive than
search().

If you want to match text stored in a variable, you will need to use the
RegExp() constructor:

  var needle = "testing";
  var haystack = "This is useful for testing cookies";
  var regex = new RegExp("\\b" + needle + "\\b");
  var found = regex.test(haystack);

In the third line, needle will be interpreted as part of a regular
expression, so you'll have to make sure that it doesn't contain any
characters that have a special meaning in regular expressions. Or you
can escape them using something like this:

  function escapeRegex (str) {
      return str.replace(/[\\^$+*?.(){}[\]]|-/g, "\\$&");
  }

And once again, I wonder why the RegExp object doesn't have a built-in
method to do this...


One other thing: please don't reuse old threads for new questions. If
you have a new question, start a new thread.


- stefan

[toc] | [prev] | [next] | [standalone]


#19506 — Re: Help creating 'simple' regex

FromStefan Weiss <krewecherl@gmail.com>
Date2013-04-27 22:34 +0200
SubjectRe: Help creating 'simple' regex
Message-ID<klhcot$k9$1@news.albasani.net>
In reply to#19505
On 2013-04-27 22:27, Stefan Weiss wrote:
>       return str.replace(/[\\^$+*?.(){}[\]]|-/g, "\\$&");

Typo: return str.replace(/[\\^$+*?.(){}[\]|-]/g, "\\$&");

- stefan

[toc] | [prev] | [next] | [standalone]


#19509 — Re: Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-27 16:39 -0700
SubjectRe: Help creating 'simple' regex
Message-ID<27oon89p058st28cgicdg93c3sdcoghpth@4ax.com>
In reply to#19505
Stefan Weiss wrote:

>On 2013-04-27 19:56, Joey@still_Learning.invalid wrote:
>> I know next to nothing about regex, but I need to determine whether an
>> exact match of a string within a string exists. My current code, which
>> doesn't work is:
>> 
>> firstString = 'testing';
>> testString = 'This is useful for testing cookies';
>> 
>> testTheStrings = testString.search(/\bfirstString\b/);
>
>In addition to what JJ wrote: if you're looking for an exact match and
>don't really need the word boundary matches, you could also use indexOf():
>
>  var found = testString.indexOf("testing") > -1;
>
>indexOf() can be a lot faster than a regular expression, depending on
>the implementation. This example would also match "protesting" and
>"testings". You can avoid that by searching for " testing ", if you know
>that the word will be surrounded by spaces.
>
I can't use indexOf(), because there are spaces before, after and withing
the string.

>If you do need the regex, but are only interested in a true/false result
>of the match, use the test() method instead of search():
>
>  var found = /\btesting\b/.test(testString);

Great. Thanks.
>
>It returns a boolean and is faster and arguably more expressive than
>search().
>
>If you want to match text stored in a variable, you will need to use the
>RegExp() constructor:
>
>  var needle = "testing";
>  var haystack = "This is useful for testing cookies";
>  var regex = new RegExp("\\b" + needle + "\\b");
>  var found = regex.test(haystack);
>
>In the third line, needle will be interpreted as part of a regular
>expression, so you'll have to make sure that it doesn't contain any
>characters that have a special meaning in regular expressions. Or you
>can escape them using something like this:
>
>  function escapeRegex (str) {
>      return str.replace(/[\\^$+*?.(){}[\]]|-/g, "\\$&");
>  }
>
>And once again, I wonder why the RegExp object doesn't have a built-in
>method to do this...
>
Thanks for your response. I appreciate it.
>
>One other thing: please don't reuse old threads for new questions. If
>you have a new question, start a new thread.
>
No problem. (You keep these threads a long time.) 

Thank you again.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#19512 — Re: Help creating 'simple' regex

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2013-04-28 09:20 +0200
SubjectRe: Help creating 'simple' regex
Message-ID<XnsA1B05F1BB33B3eejj99@194.109.133.133>
In reply to#19509
Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:

>>  var found = testString.indexOf("testing") > -1;

[..]

> I can't use indexOf(), because there are spaces before, after and withing
> the string.

Invalid argument.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#19513 — Re: Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-28 01:22 -0700
SubjectRe: Help creating 'simple' regex
Message-ID<4vmpn8lv41nhapq42a8q1fapqdh4qqr62h@4ax.com>
In reply to#19512
Evertjan. wrote:

>Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:
>
>>>  var found = testString.indexOf("testing") > -1;
>
>[..]
>
>> I can't use indexOf(), because there are spaces before, after and withing
>> the string.
>
>Invalid argument.

I don't agree. If the use inputs nothing, indexOf() will kick back a true,
when indeed the input is false.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#19514 — Re: Help creating 'simple' regex

FromStefan Weiss <krewecherl@gmail.com>
Date2013-04-28 11:32 +0200
SubjectRe: Help creating 'simple' regex
Message-ID<kliqbp$f8i$1@news.albasani.net>
In reply to#19513
On 2013-04-28 10:22, Joey@still_Learning.invalid wrote:
> Evertjan. wrote:
> 
>>Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:
>>
>>>>  var found = testString.indexOf("testing") > -1;
>>
>>[..]
>>
>>> I can't use indexOf(), because there are spaces before, after and withing
>>> the string.
>>
>>Invalid argument.
> 
> I don't agree. If the use inputs nothing, indexOf() will kick back a true,
> when indeed the input is false.

I don't understand that sentence. Anyway, indexOf() by itself doesn't
return true or false, it returns the position of the first match, or -1
if there is no match. Hence the "> -1" in the example.

If the search string is empty, it will always match, because every
possible haystack string contains the empty string. That's a feature,
not a bug. If you don't want that, don't search for an empty string.

Spaces are treated exactly the same as any other character. I only
mentioned them because they are often used as word delimiters.

 haystack          | needle        | indexOf()      |  indexOf > -1
-------------------+---------------+----------------+---------------
 "abcde"           | "ab"          | 0              | true
 "abcde"           | "cd"          | 2              | true
 "abcde"           | "ae"          | -1             | false
 ""                | ""            | 0              | true
 "abcde"           | ""            | 0              | true
 ""                | "cd"          | -1             | false
 " "               | ""            | 0              | true
 " "               | " "           | 0              | true
 " a b c d e "     | "c d"         | 5              | true
 " a b c d e "     | " c d "       | 4              | true
 " a b c d e "     | " "           | 0              | true

Whenever you find yourself looking for an exact substring, indexOf() is
the first thing you should think about. You will only need a regex if
there are additional requirements, like: the needle must begin and end
at ANY word boundary, or the number of spaces in the needle is variable.


- stefan

[toc] | [prev] | [next] | [standalone]


#19520 — Re: Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-28 09:53 -0700
SubjectRe: Help creating 'simple' regex
Message-ID<9bkqn8hmr8a2uli9adse19j9ld7ohpf4tj@4ax.com>
In reply to#19514
Stefan Weiss wrote:

>On 2013-04-28 10:22, Joey@still_Learning.invalid wrote:
>> Evertjan. wrote:
>> 
>>>Joey@still_Learning.invalid wrote on 28 apr 2013 in comp.lang.javascript:
>>>
>>>>>  var found = testString.indexOf("testing") > -1;
>>>
>>>[..]
>>>
>>>> I can't use indexOf(), because there are spaces before, after and withing
>>>> the string.
>>>
>>>Invalid argument.
>> 
>> I don't agree. If the use inputs nothing, indexOf() will kick back a true,
>> when indeed the input is false.
>
>I don't understand that sentence. 

Sorry, late at night entry. If the user elects to proceed without entering
anything, it returns a value instead of -1.

>Anyway, indexOf() by itself doesn't
>return true or false, it returns the position of the first match, or -1
>if there is no match. Hence the "> -1" in the example.

Agreed. I think in terms that -1 as false and any positive value is true. 
>
>If the search string is empty, it will always match, because every
>possible haystack string contains the empty string. That's a feature,
>not a bug. If you don't want that, don't search for an empty string.

That would always be my preference, but some of my users are damned-fools
who hit the 'do-it' button without thinking. I want to catch their error.
>
>Spaces are treated exactly the same as any other character. I only
>mentioned them because they are often used as word delimiters.
>
> haystack          | needle        | indexOf()      |  indexOf > -1
>-------------------+---------------+----------------+---------------
> "abcde"           | "ab"          | 0              | true
> "abcde"           | "cd"          | 2              | true
> "abcde"           | "ae"          | -1             | false
> ""                | ""            | 0              | true
> "abcde"           | ""            | 0              | true
> ""                | "cd"          | -1             | false
> " "               | ""            | 0              | true
> " "               | " "           | 0              | true
> " a b c d e "     | "c d"         | 5              | true
> " a b c d e "     | " c d "       | 4              | true
> " a b c d e "     | " "           | 0              | true
>
>Whenever you find yourself looking for an exact substring, indexOf() is
>the first thing you should think about. You will only need a regex if
>there are additional requirements, like: the needle must begin and end
>at ANY word boundary, or the number of spaces in the needle is variable.
>
We have seen our users (who are medical professionals!) do the dumbest
things imaginable. They have been to enter one or more spaces preceding
and within the input strings, etc. : '  Jane  Doe  ' is typical. An even
more typical example is the user entering 'test' for one string and
'testing' or 'test id' or something similar for the test string. In either
example, indexOf() correctly returns a positive value, but it's an
erroneous response re my task.

I just answered JJ's response with an amplified explanation.

Thanks for your input. 
-- 
Joey

[toc] | [prev] | [next] | [standalone]


#19521 — Re: Help creating 'simple' regex

FromStefan Weiss <krewecherl@gmail.com>
Date2013-04-28 19:41 +0200
SubjectRe: Help creating 'simple' regex
Message-ID<kljn0a$b20$1@news.albasani.net>
In reply to#19520
On 2013-04-28 18:53, Joey@still_Learning.invalid wrote:
> Stefan Weiss wrote:
>>If the search string is empty, it will always match, because every
>>possible haystack string contains the empty string. That's a feature,
>>not a bug. If you don't want that, don't search for an empty string.
> 
> That would always be my preference, but some of my users are damned-fools
> who hit the 'do-it' button without thinking. I want to catch their error.

Not a problem; you have to validate all user input anyway.

  var str = prompt("Search for:").trim();  // or equivalent fallback
  if (str) {
        if (moreValidations(str)) {
            // perform search
        } else {
            // display warning
        }
  } else {
        // do nothing, treating no input as abort
  }

> We have seen our users (who are medical professionals!) do the dumbest
> things imaginable. They have been to enter one or more spaces preceding
> and within the input strings, etc. : '  Jane  Doe  ' is typical.

So you're not looking to match exact strings... good to know.
If this is for a name search, you'll probably want separate fields for
first and last names.

trim() takes care of the spaces on both sides in "  Jane  Doe  ", but if
you want to account for the spaces in the middle, you'll have to do more
work than \binput\b.

Even the \b markers can lead to incorrect matching in certain cases. For
example, \bRhys\b would incorrectly match Rhys-Davies. There are other
gotchas with \b as well (O'Neill, LeFêvre, ... all contain word boundaries).

In any case, if you're dynamically building a regex from user input, you
can start from the "new RegExp()" example I posted earlier.


- stefan

[toc] | [prev] | [next] | [standalone]


#19525 — Re: Help creating 'simple' regex

From"Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Date2013-04-28 23:51 -0700
SubjectRe: Help creating 'simple' regex
Message-ID<su5sn8h059gira73c24q0shbe32o0kdnfl@4ax.com>
In reply to#19521
Stefan Weiss wrote:

>On 2013-04-28 18:53, Joey@still_Learning.invalid wrote:
>> Stefan Weiss wrote:
>>>If the search string is empty, it will always match, because every
>>>possible haystack string contains the empty string. That's a feature,
>>>not a bug. If you don't want that, don't search for an empty string.
>> 
>> That would always be my preference, but some of my users are damned-fools
>> who hit the 'do-it' button without thinking. I want to catch their error.
>
>Not a problem; you have to validate all user input anyway.
>
>  var str = prompt("Search for:").trim();  // or equivalent fallback
>  if (str) {
>        if (moreValidations(str)) {
>            // perform search
>        } else {
>            // display warning
>        }
>  } else {
>        // do nothing, treating no input as abort
>  }
>
>> We have seen our users (who are medical professionals!) do the dumbest
>> things imaginable. They have been to enter one or more spaces preceding
>> and within the input strings, etc. : '  Jane  Doe  ' is typical.
>
>So you're not looking to match exact strings... good to know.
>If this is for a name search, you'll probably want separate fields for
>first and last names

Actually, my users are selecting files to upload and I'm trying to
determine whether or not the name they entered is contained within the
filenames.
-- 
Joey

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web