最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Confusion regarding regex - Stack Overflow

programmeradmin2浏览0评论

I just found this regex in JavaScript

var str=input.replace(/\0/g, "\\0");

Can you please explain me what does it mean? What is the meaning of /\0/g and \\0?

I just found this regex in JavaScript

var str=input.replace(/\0/g, "\\0");

Can you please explain me what does it mean? What is the meaning of /\0/g and \\0?

Share Improve this question edited Oct 26, 2010 at 14:19 Tim Pietzcker 337k59 gold badges518 silver badges571 bronze badges asked Oct 26, 2010 at 13:20 Rocky SinghRocky Singh 15.4k31 gold badges106 silver badges146 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

\0 is the null character.

/\0/g is a pattern that will match all instances of the null character.

"\\0" is a string that will be displayed as "\0", since the first backslash acts as an escape character for the second backslash.

So this line of code replaces all instances of the null character (which is normally unreadable, unless you use a hex viewer) in the string input and replaces them with the human-readable string "\0", then stores the result in the string str.

It replaces null characters (\0 - Unicode 0x0) in the string with a backslash (\)followed by a 0.

var s = "asd0asd\x00asd";
console.log(s);
s = s.replace(/\0/g, "\\0");
console.log(s);

And the output is:

asd0asd�asd
asd0asd\0asd

发布评论

评论列表(0)

  1. 暂无评论