Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

javascript - Uncaught SyntaxError: Unexpected identifier on strings with quotes and backticks

I have value from javascript here which is a json string.

<script>
  var data = '[{"text":"Lorem's ipsum dolor sit amet"},{"text":"consectetur`s adipiscing elit"}]';
  console.log(data);
</script>

The above code has error Uncaught SyntaxError: Unexpected identifier

I also used backtick instead of single/double quote, but it has same error

<script>
  var data = `[{"text":"Lorem's ipsum dolor sit amet"},{"text":"consectetur`s adipiscing elit"}]`;
  console.log(data);
</script>

How can I do avoid the error? Given that the json string has single quote from Lorem's and backtick quote from consectetur?

question from:https://stackoverflow.com/questions/65898775/uncaught-syntaxerror-unexpected-identifier-on-strings-with-quotes-and-backticks

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can escape the single quote using backslash ':

var data = '[{"text":"Lorem's ipsum dolor sit amet"},{"text":"consectetur`s adipiscing elit"}]';
console.log(data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...