简体   繁体   English

JavaScript中的字符转义序列

[英]Character escape sequence in javascript

Just noticed that some strings (taken from an array created from a m3u playlist file) won't work due to a malformed hexadecimal character escape sequence. 刚刚注意到,由于格式错误的十六进制字符转义序列,某些字符串(取自m3u播放列表文件创建的数组)将不起作用。

var strArray =  [
"#EXTM3U",
"C:\music\X Marks the Pedwalk - Desolation.mp3", //fine
"#EXTINF:287,Xandria - Ginger Sunset Expire", //fine
"C:\music\andria - Ginger Sunset Expire.mp3", //fine
"C:\music\xandria - Ginger Sunset Expire.mp3", // FAILS
"C:\\music\\xandria - Ginger Sunset Expire.mp3" //fine
]

alert (strArray);

I can get around it with escape slashes. 我可以用逃脱斜线解决它。 But my question is what is actually causing the error. 但是我的问题是到底是什么导致了错误。 I thought it might be something to do with \\x but that would mean the first track would also fail. 我认为这可能与\\ x有关,但这意味着第一首曲目也会失败。 So I'm a little bit confused. 所以我有点困惑。

I thought it might be something to do with \\x but that would mean the first track would also fail. 我认为这可能与\\ x有关,但这意味着第一首曲目也会失败。

No, because x and X are not the same character. 否,因为xX不是同一字符。 :-) \\x (with the x in lower case) is special in string literals , \\X (with the X in upper case) is not. :-) \\x (小写的x在字符串文字中是特殊的\\X (小写的X在字符串文字中是特殊的

Best practice is to always escape backslashes that are meant to really be backslashes as opposed to the beginning of an escape sequence. 最佳做法是始终转义实际上是反斜杠的反斜杠,而不是转义序列的开始。 Otherwise, you will trip yourself up. 否则,您绊倒自己。

You can replace backslashes with the function str_replace.. 您可以将反斜杠替换为功能str_replace。

<?php

$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Produce: <body text='black'>

http://php.net/manual/es/function.str-replace.php http://php.net/manual/es/function.str-replace.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM