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


Groups > comp.lang.javascript > #19514

Re: Help creating 'simple' regex

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Help creating 'simple' regex
Date 2013-04-28 11:32 +0200
Organization albasani.net
Message-ID <kliqbp$f8i$1@news.albasani.net> (permalink)
References (1 earlier) <gt3on8tkhr5iu14im8cprl1jtre1e4pm7u@4ax.com> <klhcb9$vdr$1@news.albasani.net> <27oon89p058st28cgicdg93c3sdcoghpth@4ax.com> <XnsA1B05F1BB33B3eejj99@194.109.133.133> <4vmpn8lv41nhapq42a8q1fapqdh4qqr62h@4ax.com>

Show all headers | View raw


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

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