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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RegExp(String) escape?

    I tried hiding 'special characters' (such as '[') with "\", or "\\", or "\\\\", for the 'line' parameter, but get: [Error: Expected ')' in regular expression]. Is that the wrong escape method, or...?

    Code:
    <body onload="sPrompt('HoHoHo')">
    <script type="text/javascript">
    
    // Periodic Strings play...
    function periodic(line){
    	var i = 0;
    	do{++i} while(!RegExp("^(" + line.substr(0, i) + ")+$").test(line));
    	return i;}
    
    
    // test
    function sPrompt(line){
    	if(line = prompt(line + " => " + periodic(line), ""))
    		sPrompt(line);}
    </script>
    </body>
    *this message will self destruct in n-seconds*

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah... now that I think, the problem must be that I'm isolating the escape character as a substring--which in the expression would of course cause error. Woops!
    *this message will self destruct in n-seconds*

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, that was it.

    Code:
    function periodic(line){
    	var i = 0, spechar = /([\$\(\)\*\+\.\[\]\?\\\/\^\{\}\|])/g;
    	do{++i} while(!RegExp("^(" + line.substr(0, i).replace(spechar, "\\$1") + ")+$").test(line));
    	return i;}
    *this message will self destruct in n-seconds*


  •  

    Posting Permissions

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