why backslash(\) in a string is giving error in console

旧巷老猫 提交于 2020-01-17 14:23:07

问题


i have a string like

"C:\projects\cisco\iwan_staging_enc\enterprise-network-controller\ui-plugins\iwan"

when i paste into console and press enter, it is giving following error as

Uncaught SyntaxError: Invalid Unicode escape sequence

whats wrong here

Thanks

nageshwar


回答1:


The \u is the start of a unicode escape sequence, in your string you have a \u not followed by four hex numbers which is the format of unicode escape sequence \uxxxx. See

"C:\projects\cisco\iwan_staging_enc\enterprise-network-controller\u0050i-plugins\iwan"

\u0050 id P

Also there there are other types of escapes, so for instance if you had a \n somewhere in there you would get a newline

"C:\new projects\cisco\iwan_staging_enc\enterprise-network-controller\u0050i-plugins\iwan"

So if you do not want avoid these escape sequences escape the \s in the string with a slash before it.

"C:\\projects\\cisco\\iwan_staging_enc\\enterprise-network-controller\\ui-plugins\\iwan"



回答2:


Since backslash is an escape character your string should be modified to:

"C:\\projects\\cisco\\iwan_staging_enc\\enterprise-network-controller\\ui-plugins\\iwan"

Please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Escape_notation



来源:https://stackoverflow.com/questions/38722295/why-backslash-in-a-string-is-giving-error-in-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!