#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Regex URL in Javascript


    Hi, I am trying to do a regex match in javascript and I'm stuggling.
    I can get the regex to match using online regex checkers and in C# code but not in javascript.

    I would like to match the following exampleURL, where the 67890 can be any number:

    Code:
    exampleURL = "/pages/67890.aspx";
    
    var reg = new RegExp("pages\/\d*\.aspx");
    
    if (reg.test(exampleURL)) {
    
    	alert('Match');
    
    }

    Is appears to be the "pages\/" part of the regex that is causing the problems, but I'm not sure why.

    Thanks.
  2. #2
  3. Confusing Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,876
    Rep Power
    9569
    The backslashes in the string need to be escaped because they're in a string.
    javascript Code:
    var reg = new RegExp("pages\\/\\d*\\.aspx");

    Or you can use the shorthand syntax.
    javascript Code:
    var reg = /pages\/\d*\.aspx/;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0
    Brilliant - thanks!

IMN logo majestic logo threadwatch logo seochat tools logo