www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] RegExp is not working right...

  1. #1
    Join Date
    May 2008
    Posts
    80

    resolved [RESOLVED] RegExp is not working right...

    Hello,

    I'm trying to use the following pattern with RegExp, but it doesn't seem to work right. When I take the same expression and use it directly, the expression works correctly.

    The expression is designed to find numbers or numbers with letter at the end.
    The example below should not find any match in the string.
    The RegExp example finds "ld". Why? What am I doing wrong?

    HTML Code:
    var str = "printf(\"Hello World\");";
    	
    var myPattern = new RegExp("([^\w][\d]+[\w]*)|(^[\d]+[\w]*)");
    alert(str.search(myPattern));
    	
    alert(str.search(/([^\w][\d]+[\w]*)|(^[\d]+[\w]*)/));
    Thank you.

  2. #2
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Code:
    alert(str.search(/^\d+\w$/));
    
    ^     begining of string
    \d+   one or more numbers 
    \w    one number or letter only
    $     END OF STRING

  3. #3
    Join Date
    May 2008
    Posts
    80
    Quote Originally Posted by Nedals View Post
    Code:
    alert(str.search(/^\d+\w$/));
    
    ^     begining of string
    \d+   one or more numbers 
    \w    one number or letter only
    $     END OF STRING
    I'm sorry that's not what I asked.
    My expression, when used through RegExp doesn't work like it should, and when used directly with / / it works great, why???

  4. #4
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Hmm - I guess 'myPattern' does not interpolate within an alert
    Code:
    var str = "printf(\"Hello World\");";
    var myPattern = new RegExp("([^\w][\d]+[\w]*)|(^[\d]+[\w]*)");
    
    var result = str.search(myPattern);
    alert(str.search(result));
    Just for reference,
    Your regex will match a lot more than ONLY numbers follows by one letter or number.

  5. #5
    Join Date
    Feb 2006
    Posts
    2,927
    When you create a RegExp from a string with new RegExp('string') you have to be careful to escape the escapes.

    var myPattern = new RegExp("([^\\w][\\d]+[\\w]*)|(^[\\d]+[\\w]*)")

    Otherwise, the RegExp you create is: /([^w][d]+[w]*)|(^[d]+[w]*)/;// from alert(myPattern)

    So you returned the first character that is not a 'w' and is followed by some number of 'd's followed by zero or more 'w's- 'ld'.
    Last edited by mrhoo; 01-02-2009 at 09:55 PM.

  6. #6
    Join Date
    May 2008
    Posts
    80
    Quote Originally Posted by mrhoo View Post
    When you create a RegExp from a string with new RegExp('string') you have to be careful to escape the escapes.

    var myPattern = new RegExp("([^\\w][\\d]+[\\w]*)|(^[\\d]+[\\w]*)")

    Otherwise, the RegExp you create is: /([^w][d]+[w]*)|(^[d]+[w]*)/;// from alert(myPattern)

    So you returned the first character that is not a 'w' and is followed by some number of 'd's followed by zero or more 'w's- 'ld'.
    Of course....Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles