Finding a number in string - RegExp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    Finding a number in string - RegExp

    Hi,

    How can I check if a number exists by itself in this string by using
    the RegExp object?

    ---

    var mystring = "11,111,01,011" ;
    var match = "1";
    var re = new RegExp( match );
    var isFound = re.test( mystring ) );

    ---

    Running this code returns 'true' which is not what I want since number
    one doesn't exist by itself. I need to use the "match" variable since
    it will change depending on user input.

    I can only get it to work without variables in the expression. E.g.

    var re = new RegExp( /\b1\b/ );

    Many thanks

  • Randy Webb

    #2
    Re: Finding a number in string - RegExp

    Nick said the following on 8/13/2005 5:46 PM:[color=blue]
    > Hi,
    >
    > How can I check if a number exists by itself in this string by using
    > the RegExp object?
    >
    > ---
    >
    > var mystring = "11,111,01,011" ;
    > var match = "1";
    > var re = new RegExp( match );
    > var isFound = re.test( mystring ) );
    >
    > ---
    >
    > Running this code returns 'true' which is not what I want since number
    > one doesn't exist by itself. I need to use the "match" variable since
    > it will change depending on user input.
    >
    > I can only get it to work without variables in the expression. E.g.
    >
    > var re = new RegExp( /\b1\b/ );
    >
    > Many thanks
    >[/color]

    var mystring = "11,111,01,011" ;
    var bound1 = "\b"
    var bound2 = "\b"
    var match = "1"
    var re = new RegExp(bound1 + match + bound2);
    var isFound = re.test( mystring );
    alert(isFound)


    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • Nick

      #3
      Re: Finding a number in string - RegExp

      Thanks Randy,
      However, this code doesn't seem to produce the correct result if I for
      example set match to "11" instead.
      It returns false no matter what I set. Any ideas?

      Comment

      • Stephen Chalmers

        #4
        Re: Finding a number in string - RegExp

        Nick <nick_1979@hotm ail.com> wrote in message news:1123969607 .057512.46170@g 44g2000cwa.goog legroups.com...[color=blue]
        > Hi,
        >
        > How can I check if a number exists by itself in this string by using
        > the RegExp object?
        >
        > ---
        >
        > var mystring = "11,111,01,011" ;
        > var match = "1";
        > var re = new RegExp( match );
        > var isFound = re.test( mystring ) );
        >
        > ---
        >
        > Running this code returns 'true' which is not what I want since number
        > one doesn't exist by itself. I need to use the "match" variable since
        > it will change depending on user input.
        >
        > I can only get it to work without variables in the expression. E.g.
        >
        > var re = new RegExp( /\b1\b/ );
        >
        > Many thanks
        >[/color]

        Try:

        var match = '\\b1\\b';
        var re = new RegExp( match );

        --
        Stephen Chalmers


        Comment

        • Nick

          #5
          Re: Finding a number in string - RegExp

          Bingo!
          The double backslashes made the trick!

          Thanks a lot! :)
          Nick

          Comment

          • Dr John Stockton

            #6
            Re: Finding a number in string - RegExp

            JRS: In article <1123969607.057 512.46170@g44g2 000cwa.googlegr oups.com>,
            dated Sat, 13 Aug 2005 14:46:47, seen in news:comp.lang. javascript, Nick
            <nick_1979@hotm ail.com> posted :
            [color=blue]
            >How can I check if a number exists by itself in this string by using
            >the RegExp object?[/color]


            You need to define "number" and "by itself" unambiguously; only then can
            a suitable RegExp be chosen.

            Does "number" mean digit, decimal digit, one or more of those, with a
            sign, with a decimal point, with an exponent part, octal or hexadecimal
            number, within the range of type Number ??

            Does "by itself" mean that there must be no other characters in the
            string, no other "printing" characters?; or is it only the characters on
            each side that matter?; what if it is at the beginning or end of the
            string?

            If it is the characters on each side that matter, which are allowed?
            The numbers of the properties to the east of to mine are of the form 97X
            & 97Y.

            Is the 5 in a5a "by itself"? Is punctuation allowed, but not letters?
            How about .5 - is that a half or a five?

            <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Randy Webb

              #7
              Re: Finding a number in string - RegExp

              Dr John Stockton said the following on 8/14/2005 9:40 AM:[color=blue]
              > JRS: In article <1123969607.057 512.46170@g44g2 000cwa.googlegr oups.com>,
              > dated Sat, 13 Aug 2005 14:46:47, seen in news:comp.lang. javascript, Nick
              > <nick_1979@hotm ail.com> posted :
              >
              >[color=green]
              >>How can I check if a number exists by itself in this string by using
              >>the RegExp object?[/color]
              >
              >
              >
              > You need to define "number" and "by itself" unambiguously; only then can
              > a suitable RegExp be chosen.[/color]

              Those two seem to be self definitive to anyone who read the post other
              than you.
              [color=blue]
              > Does "number" mean digit, decimal digit, one or more of those, with a
              > sign, with a decimal point, with an exponent part, octal or hexadecimal
              > number, within the range of type Number ??[/color]

              From the example, and following posts, that again is self-evident to
              anyone but you.
              [color=blue]
              > Does "by itself" mean that there must be no other characters in the
              > string, no other "printing" characters?; or is it only the characters on
              > each side that matter?; what if it is at the beginning or end of the
              > string?[/color]

              <sigh> Again, that was self-evident. He wanted to find 1, but not 11 or
              a1a or any other form, he wanted 1 and only 1. That was obvious to
              anyone but you.
              [color=blue]
              > If it is the characters on each side that matter, which are allowed?[/color]

              Read above. You seem to enjoy trying to make things harder than they are.
              [color=blue]
              > The numbers of the properties to the east of to mine are of the form 97X
              > & 97Y.
              >
              > Is the 5 in a5a "by itself"?[/color]

              Only to someone like you.
              [color=blue]
              > Is punctuation allowed, but not letters?[/color]

              Geez, you just don't get it do you?
              [color=blue]
              > How about .5 - is that a half or a five?[/color]

              Neither, it is the numeral five preceded by a decimal. Whether that is
              half, five, or something else is entirely dependent on the base in use.
              ..5 in base 8 is neither Five nor Half. In fact, it is neither in any
              base above 5 but not 10.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

              Comment

              Working...