* fix(learn): address escaped backticks Addresses the instances of escaped backticks - where a backtick is preceded by a backslash. In most cases, this was left over from the old parser. In some cases, a backtick was intended to be wrapped in code tags and has been adjusted accordingly. This issue came to light due to a bug in the translation flow on Crowdin. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * fix: EVEN MORE :( :( :( Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * fix: backslash nightmares Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * fix: When you wish upon a ******* Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * fix(curriculum): md error introduced by formatter * fix(curriculum): remove extra `s * fix: restore quote symbol Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * fix: Typo Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * fix: apply review changes Applying review feedback from call with @RandellDawson. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * fix: markdown does weird stuff sometimes Can't stick backticks together - use code. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
67 lines
1.8 KiB
Markdown
67 lines
1.8 KiB
Markdown
---
|
|
id: 56533eb9ac21ba0edf2244b5
|
|
title: Escaping Literal Quotes in Strings
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/c2QvgSr'
|
|
forumTopicId: 17568
|
|
dashedName: escaping-literal-quotes-in-strings
|
|
---
|
|
|
|
# --description--
|
|
|
|
When you are defining a string you must start and end with a single or double quote. What happens when you need a literal quote: `"` or `'` inside of your string?
|
|
|
|
In JavaScript, you can <dfn>escape</dfn> a quote from considering it as an end of string quote by placing a <dfn>backslash</dfn> (<code>\\</code>) in front of the quote.
|
|
|
|
`var sampleStr = "Alan said, \"Peter is learning JavaScript\".";`
|
|
|
|
This signals to JavaScript that the following quote is not the end of the string, but should instead appear inside the string. So if you were to print this to the console, you would get:
|
|
|
|
`Alan said, "Peter is learning JavaScript".`
|
|
|
|
# --instructions--
|
|
|
|
Use <dfn>backslashes</dfn> to assign a string to the `myStr` variable so that if you were to print it to the console, you would see:
|
|
|
|
`I am a "double quoted" string inside "double quotes".`
|
|
|
|
# --hints--
|
|
|
|
You should use two double quotes (`"`) and four escaped double quotes (`\"`).
|
|
|
|
```js
|
|
assert(code.match(/\\"/g).length === 4 && code.match(/[^\\]"/g).length === 2);
|
|
```
|
|
|
|
Variable myStr should contain the string: `I am a "double quoted" string inside "double quotes".`
|
|
|
|
```js
|
|
assert(/I am a "double quoted" string inside "double quotes(\."|"\.)$/.test(myStr));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --after-user-code--
|
|
|
|
```js
|
|
(function(){
|
|
if(typeof myStr === 'string') {
|
|
console.log("myStr = \"" + myStr + "\"");
|
|
} else {
|
|
console.log("myStr is undefined");
|
|
}
|
|
})();
|
|
```
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
var myStr = ""; // Change this line
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
var myStr = "I am a \"double quoted\" string inside \"double quotes\".";
|
|
```
|