[英]Javascript - How to Escape Double quotes from Dynamic string content, Unexpected identifier error
Have used to Bind Login User name from portal to a Javascript variable.曾经将登录用户名从门户绑定到 Javascript 变量。 User name contains either SingleQuote or DiubleQuote sometimes.用户名有时包含 SingleQuote 或 DiubleQuote。
My Javascript code failed to read string with DiubleQuote.我的 Javascript 代码无法使用 DiubleQuote 读取字符串。
EX:前任:
var userName = "Happy and "Care" Ltd"; - error: unknown: Unexpected token, expected ";"
var userName1 = 'Happy and Care's Ltd'; - error: unknown: Unexpected token, expected ";"
You can use backticks `` like this:您可以像这样使用反引号``:
var userName = `Happy and "Care" Ltd`; var userName1 = `Happy and Care's Ltd`; console.log(userName) console.log(userName1)
Backslash is used as a escape character.反斜杠用作转义字符。 For example you can use like this:例如,您可以像这样使用:
var userName1 = 'Happy and Care\'s Ltd';
Found Solution Finally...终于找到解决方案了...
var userName = /"John "Deo" Peter"/; var resulrtStr = userName.toString().replace(/\\|\//g,'').replace(/['"]+/g, '');
Hope this would help someone someday....希望有一天这会对某人有所帮助....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.