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


Groups > comp.lang.javascript > #19505

Re: Help creating 'simple' regex

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Help creating 'simple' regex
Date 2013-04-27 22:27 +0200
Organization albasani.net
Message-ID <klhcb9$vdr$1@news.albasani.net> (permalink)
References <mq0kk8ljpnig2uhebno0ogrb2tc2gqb4oo@4ax.com> <gt3on8tkhr5iu14im8cprl1jtre1e4pm7u@4ax.com>

Show all headers | View raw


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

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