[thelist] Javascript regular expression question

Lee Kowalkowski lee.kowalkowski at googlemail.com
Thu Apr 19 14:59:46 CDT 2007

On 19/04/07, Judah McAuley <judah at wiredotter.com> wrote:
> Howdy, I'm trying to clean up text that is pasted into form fields in an
> application. In particular, I'm trying to make sure I strip out all the
> control characters. Generically, the regexes I'm trying to implement are:
> [\x00-\x09]
> [\x0B-\x0C]
> [\x0E-\x1F]
> [\x7F-\x9F]

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?

> 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(" "))


More information about the thelist mailing list