Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting muddled with RegExp

    Hi, I'm really new to Javascript and am trying to work my way through someone else's code.. But I'm getting really lost with all the brackets and backslashes involved in this RegExp.

    Code:
    var rtRegexRequired = new RegExp ("(\\(Required\\))","gi");
    I just need to know what this RegExp is looking for..

    Thanks.
    Ic.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Are you sure that it is not:-

    Code:
    var rtRegexRequired = new RegExp ("\(\(Required\)\)","gi");
    If so the regex is looking for

    ((Required)) global match and case insensitive.
    The backslashes \ are escape characters so that the ( and ) are interpreted as literals.



    Label on Sainsbury's peanuts -- "Warning: contains nuts."

  • #3
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Are you sure that it is not:-
    Yeah, see I think thats where I'm getting confused, I expected it to be looking for ((Required)), but its really not.. Wonder if the original coder just made a typo..

    That is exactly the code in the js, so as it is, what's it looking for?

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Okay, let's try explaining then: Have a look at the structure of it:
    Code:
    new RegExp ("(\\(Required\\))","gi");
    Okay, so "(\\(Required\\))" is a string literal, right? That means the reverse solidi (a.k.a. backslashes) are string escapes, and you need to perform the escaping to know what the actual pattern looks like.
    So, the actual pattern looks like this: (\(Required\)) (where the reverse solidi are regex escapes), and the code indeed could just as well have been written as
    Code:
    var rtRegexRequired = /(\(Required\))/gi;
    What does that pattern do, then? Well, its matching for a string that looks like this: "(Required)" case insensitively. The entire string it matches is enclosed in a capturing group, however, so the string will be captured. And finally, the pattern has a global flag on it, which will affect the behaviour of re.exec, str.match and str.replace.
    Last edited by liorean; 09-02-2008 at 04:34 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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