...

View Full Version : RegExp(String) escape?



codegoboom
03-13-2005, 06:11 AM
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...?


<body onload="sPrompt('HoHoHo')">
<script type="text/javascript">

// Periodic Strings (http://acm.uva.es/p/v4/455.html) 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>

codegoboom
03-13-2005, 08:08 AM
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! :)

codegoboom
03-13-2005, 10:05 PM
Yes, that was it. :cool:


function periodic(line){
var i = 0, spechar = /([\$\(\)\*\+\.\[\]\?\\\/\^\{\}\|])/g;
do{++i} while(!RegExp("^(" + line.substr(0, i).replace(spechar, "\\$1") + ")+$").test(line));
return i;}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum