Help with RegExp

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

    Help with RegExp

    Hello,

    I am trying to create a regexp to express non letters and space.
    I tried using:

    var myreg = new RegExp ("[^\\sA-Za-z]");

    However, it is not working.
    Basically I want to allow only words with letters in a textfield,
    space is ok, but no special characters such as $%^ or numbers such as
    1234.

    If I use: var myreg = new RegExp ("[^A-Za-z]");
    then "Hello" is ok, but not "Hello There" because there is a space
    between Hello and There. I want to allow:

    Hello
    Hello There
    Hello there how are you

    and I do not want to allow any special characters such as #$%^ or
    numbers.

    Thank you!
  • Lasse Reichstein Nielsen

    #2
    Re: Help with RegExp

    alii@paul.rutge rs.edu (Syed Ali) writes:
    [color=blue]
    > I am trying to create a regexp to express non letters and space.
    > I tried using:[/color]

    Matches only English letters and space:

    /^[a-z\s]*$/i

    Matches strings with non-letter, non-space character:
    /[^a-z\s]/i
    [color=blue]
    > var myreg = new RegExp ("[^\\sA-Za-z]");[/color]

    that would be equivalent to the latter.
    [color=blue]
    > However, it is not working.[/color]

    "not working" isn't helpful. How does it fail? It should recognize
    only strings that contain a non-letter, non-space character.

    So
    if (myreg.test(str ing)) {
    // string is illegal
    }
    should work.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Michael Winter

      #3
      Re: Help with RegExp

      Syed Ali wrote on 18 Dec 2003 at Thu, 18 Dec 2003 21:27:37 GMT:
      [color=blue]
      > Hello,
      >
      > I am trying to create a regexp to express non letters and space.
      > I tried using:
      >
      > var myreg = new RegExp ("[^\\sA-Za-z]");
      >
      > However, it is not working.[/color]

      I don't quite know why that doesn't work, but I'm too tired to think
      about it properly anyway. :)
      [color=blue]
      > Basically I want to allow only words with letters in a
      > textfield, space is ok, but no special characters such as $%^ or
      > numbers such as 1234.
      >
      > If I use: var myreg = new RegExp ("[^A-Za-z]");
      > then "Hello" is ok, but not "Hello There" because there is a
      > space between Hello and There. I want to allow:
      >
      > Hello
      > Hello There
      > Hello there how are you
      >
      > and I do not want to allow any special characters such as #$%^
      > or numbers.[/color]

      The expression, /[^\sa-z]/i.test( string ), should evaluate to true
      if any character that is not a letter (either case) or space exists
      in 'string'. If it evaluates to false, all of the characters are
      valid. For example,

      Legal example:

      if (/[^\sa-z]/i.test( 'Hello there' )) {
      // Illegal characters [not executed]
      } else {
      // All legal characters [executed]
      }


      Illegal example:

      if (/[^\sa-z]/i.test( 'No-one can beat me' )) {
      // Illegal characters [executed: hyphen]
      } else {
      // All legal characters [not executed]
      }

      By the way, using literal regular expressions should be more
      efficient. Always use a literal when you have a constant pattern.

      I did test it, but blame the aforementioned tiredness if I did make
      a mistake.

      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

      Comment

      • Janwillem Borleffs

        #4
        Re: Help with RegExp


        "Lasse Reichstein Nielsen" <lrn@hotpop.com > schreef in bericht
        news:wu8tois8.f sf@hotpop.com.. .[color=blue]
        >[color=green]
        > > var myreg = new RegExp ("[^\\sA-Za-z]");[/color]
        >
        > that would be equivalent to the latter.
        >[color=green]
        > > However, it is not working.[/color]
        >
        > "not working" isn't helpful. How does it fail? It should recognize
        > only strings that contain a non-letter, non-space character.
        >[/color]

        It's obvious why it fails; the preceding backslash causes the second
        backslash to be interpreted as a literal.

        This should work as expected:
        var myreg = new RegExp ("[^\sA-Za-z]");


        JW



        Comment

        • Michael Winter

          #5
          Re: Help with RegExp

          Janwillem Borleffs wrote on 18 Dec 2003 at Thu, 18 Dec 2003
          23:04:25 GMT:
          [color=blue]
          > It's obvious why it fails; the preceding backslash causes the
          > second backslash to be interpreted as a literal.
          >
          > This should work as expected:
          > var myreg = new RegExp ("[^\sA-Za-z]");[/color]

          I thought that, however (from Netscape's JavaScript reference,
          v1.3):

          For example, the following are equivalent:

          re = new RegExp("\\w+")
          re = /\w+/

          I haven't tested which of the two, double or single slash (both when
          quoted), is correct.

          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Help with RegExp

            "Janwillem Borleffs" <jw@jwscripts.c om> writes:
            [color=blue]
            > "Lasse Reichstein Nielsen" <lrn@hotpop.com > schreef in bericht
            > news:wu8tois8.f sf@hotpop.com.. .[color=green]
            >>[color=darkred]
            >> > var myreg = new RegExp ("[^\\sA-Za-z]");[/color][/color][/color]
            ....[color=blue]
            > It's obvious why it fails; the preceding backslash causes the second
            > backslash to be interpreted as a literal.[/color]

            As it should! The "\\" occurs inside a *string literal*. That means that
            the resulting string will contain
            [^\sA-Za-z]
            Turned into a regular expression, it is equivalent to
            /[^\sA-Za-z]/
            That is just what we wanted.
            [color=blue]
            > This should work as expected:
            > var myreg = new RegExp ("[^\sA-Za-z]");[/color]

            No. In a string literal, "\s" is the same as "s". That means that the
            regular expression will be equivalent to /[^sA-Za-z]/, which matches
            spaces. Check:
            myreg.test("abc def")
            It gives true, where the original poster wanted something that gave
            false.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            Working...