Reputation: 83
Have used to Bind Login User name from portal to a Javascript variable. User name contains either SingleQuote or DiubleQuote sometimes.
My Javascript code failed to read string with 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 ";"
Upvotes: 0
Views: 1230
Reputation: 83
Found Solution Finally...
var userName = /"John "Deo" Peter"/;
var resulrtStr = userName.toString().replace(/\\|\//g,'').replace(/['"]+/g, '');
Hope this would help someone someday!...
Upvotes: 1
Reputation:
Backslash is used as a escape character. For example you can use like this:
var userName1 = 'Happy and Care\'s Ltd';
Upvotes: 1
Reputation: 4647
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)
More reading about template literals
Upvotes: 1