0

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 ";"
1
  • 1
    That's part of any JS tutorial and should be easily solvable by a quick search with your preferred search provider (Google, Bing, Yahoo, ...) Commented Jan 27, 2021 at 10:09

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

10 Comments

Hello Namysh - Backtick is working only in Chrome, Edge and Firefox. the same is not supported in IE11 version. stucked again.
Backtick is working only in Chrome, Edge and Firefox. the same is not supported in IE11 version. any suggestions?
Considering the binding is dynamic you don't need to write anything then don't need any escaping no ?
yeah its Dynamic binding. when username comes like "john's garage"(single quote inbetween name) its working fine. the same is failed when username comes like "Peter "st" paul"(double quote inbetween name). when a user name contains a double quote that name becomes unreadable by js code.
Ok and how you the binding is applied ? Are you using some front-end framework ?
|
1

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

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

1 Comment

Not able to add any escape character as the Text am getting is a Dynamic binding one. sample text will be 1. "All here "IS" Well", 2. "This is User's name"
1

Found Solution Finally...

var userName = /"John "Deo" Peter"/;  
var resulrtStr = userName.toString().replace(/\\|\//g,'').replace(/['"]+/g, '');                      

Hope this would help someone someday!...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.