www.webdeveloper.com
Results 1 to 7 of 7

Thread: Javascript SIMPLE RegEX

  1. #1
    Join Date
    Jun 2009
    Posts
    26

    Javascript SIMPLE RegEX

    Hey guys,

    What is wrong with the following regular expression:

    HTML Code:
    <script language="javascript" type="text/javascript">
    
    
    document.write('\\Green Apple\\u003c\\\/a>'.match(/(.*)\\\\u003c\\\\\\\/a>/));
    
    </script>
    The ouput is: null
    I am escaping each backlash but it doesnt seem to be working...
    Last edited by J0kerz; 05-01-2011 at 06:00 PM.

  2. #2
    Join Date
    Mar 2011
    Posts
    61
    You're escaping to much. It's not really clear what the desired result is either.
    Code:
    var str = '\;:[]}}\\\">\\Green Apple\\u003c\\\/a>\\u003c\\\/div>';
    
    //unescaped:  ;:[]}}\">\Green Apple\u003c\/a>\u003c\/div>
    var re0 = /(.*?)\\u003c\\\/a>/;
    var re1 = /.*?\\u003c\\\/a>/;
    
    document.write(str+'<br>');
    document.write(str.match(re0)+'<br>');
    document.write(str.match(re1)+'<br>');
    re0.test(str);
    document.write(RegExp.$1+'<br>');

  3. #3
    Join Date
    Dec 2005
    Posts
    2,984
    You've got one trailing backslash at the end too many - it should be:
    Code:
    (.*)\\\\u003c\\\\\\/a>
    In the above there are six \ before the /a>, in your example, you've got seven.

    That being said, what in the world are you trying to do because, unless you are just practicing your regexp skills, I doubt that this is a good way of solving your problem.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  4. #4
    Join Date
    Dec 2009
    Location
    Shropshire UK
    Posts
    153
    Also Kever's picked up on (.*), which will greedily grab everything and then backtrack. Not ideal and might end up with an unintended match.

    .*? is better. The trailing non greedy? prevents that.

  5. #5
    Join Date
    Jun 2009
    Posts
    26
    Thanks guys.

    Actually I am messing with a really complicated code. The actual string is original, I did not add anything to it.

    My RegEx is correct but the actual problem seems to come from the string. I juste read that:

    Because of the string, each \\ becomes a \.
    So I got a couple of question regarding this statement:

    Why does \\ become \ in a string?

    Is there anyway to make sure \\ doesnt become \ in a string?

  6. #6
    Join Date
    Mar 2011
    Posts
    61
    You have to add a slash to every escaped special character, which are:
    ! @ # $ &#37; ^ & * ( ) + = - [ ] \ ' ; , . / { } | " : < > ? ~ _

    Which could be done with an addslashes function
    Code:
    function addslashes (str) {
     return str.replace(/[!@#\$%\^&\*\(\)\+=-\[\]\\';,\.\/\{\}\|":<>\?~_]/g, '\\$&');
    };
    There a catch though.
    In your String: \;:[]}}\\\">\\Green Apple\\u003c\\\/a>\\u003c\\\/div>

    you have escaped special character: ; \ " /
    but you also have unescaped special character: : [ ] } >

    Those unescaped ones should't have a slash added, so they shouldn't be added to the 'addslash' function.

    To be save, here is a function which only add slashes to the escaped characters in your string.
    But I can't guarantee it works correct on every string you might have.
    Code:
    function addslashes (str) {
     return str.replace(/[\\"';/]/g, '\\$&');
    };
    str = addslashes(str)

  7. #7
    Join Date
    Dec 2005
    Posts
    2,984
    Do you want to post what you're actually trying to do rather than solving this one problem for this one string, because chances are you'll be back once you come across another one.

    All that being said, the solution I posted in post #3 in this thread is the answer to your original question...one too many trailing backslashes in your regexp....but again, the probably won't help you if your actual problem is more complicated than just having to match this one specific type of string.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

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