开发者

JavaScript Replace - u0009 .... with .replace(/\u0009/g,'');

开发者 https://www.devze.com 2023-01-02 05:21 出处:网络
I\'d like to use Javascript to replace all instances of \\u009 in a string This doesn\'t seem to be working: .replace(/\\u000开发者_运维知识库9/g,\'\');

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

This doesn't seem to be working: .replace(/\u000开发者_运维知识库9/g,'');

Do I need to escape something?


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, "") );


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号