[thelist] Javascript regular expression question

Judah McAuley judah at wiredotter.com
Thu Apr 19 15:30:59 CDT 2007

Lee Kowalkowski wrote:
> Interesting, you're allowing 0A and 0D (New Line and Carriage Return)
> and all ASCII characters from space to tilde.
> You could do this to strip out the rest: str = str.replace(/[^\n\r -~]/g, "");
> Your expressions look sound as they are, but they tend to when out of
> context, how are you trying to use them?

This particular part of the application is a front end that produces an 
xml document, so the character set is actually UTF-8 and I'm allowing 
non-ascii characters, so the "strip out the rest" won't quite work, 
alas. A good idea though. Basically, I'm trying to strip out things that 
will cause problems when inserted into a CDATA block. As it turns out, 
control characters will wreak havoc with xml parsers even inside a CDATA 

>> The regex tester at regular-expressions.info tells me that
>> /[\x00-\x09]/g doesn't match my control character that I pasted in, but
>> it also doesn't tell me my regex is wrong either.
> I'm dubious, what flavour of regEx does it test?  If I enter the
> following into a browser address bar it works fine, so the syntax is
> OK as far as I can tell:
> javascript:alert(/[\x20-\x21]/.test(" "))

Hmmm...might be a problem with pasting the control character then. I was 
trying to paste in x03 This character actually ended up in an error on 
this app which is why I'm testing it. I still don't know how the end 
user got it in there, but there it is.

I'll try putting the regexes in place and see how it goes.


More information about the thelist mailing list