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


Groups > comp.lang.javascript > #19510

Re: Help creating 'simple' regex

From "Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Newsgroups comp.lang.javascript
Subject Re: Help creating 'simple' regex
Date 2013-04-27 16:39 -0700
Message-ID <8doon8d9i9q8i50fkd0fk4acbui3k19ips@4ax.com> (permalink)
References <mq0kk8ljpnig2uhebno0ogrb2tc2gqb4oo@4ax.com> <gt3on8tkhr5iu14im8cprl1jtre1e4pm7u@4ax.com> <1culsl58861ny.zc5g3yg3m79c$.dlg@40tude.net>

Show all headers | View raw


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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web