Surendar
Surendar

Reputation: 83

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. 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

Answers (3)

Surendar
Surendar

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

user15090582
user15090582

Reputation:

Backslash is used as a escape character. For example you can use like this:

var userName1 = 'Happy and Care\'s Ltd';

Upvotes: 1

Namysh
Namysh

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

Related Questions