views:

80

answers:

2

I'd like to use Javascript to replace all instances of \u009 in a string

This doesn't seem to be working: .replace(/\u0009/g,'');

Do I need to escape something?

+2  A: 

First, the question says "replace all instances of \u009 in a string".

But, the regex has replace(/\u0009/g,''); Is this a typo (different number of zeroes)?

Anyway, if the string only contains, unicode, horizontal tab characters (just one char), then the regex is fine.

If it actually contains 6 ascii chars, then the regex needs to be escaped, like so:

var oneChar     = 'Pre \u0009 post';
var sixChars    = 'Pre \\u0009 post';

//-- NOTE: If not using Firebug, replace 'console.log()' with 'alert()'.

console.log (oneChar  + ' becomes --> ' + oneChar.replace  (/\u0009/g, "") );
console.log (sixChars + ' becomes --> ' + sixChars.replace (/\\u0009/g, "") );
Brock Adams
+1  A: 

You need another escape .replace(/\\u009/g,''); :)

Fopfong
@Fopfong: You need to use code formatting, or SO tends to eat your backslashes.
Alan Moore