2018-10-10 18:03:03 -04:00
---
id: 587d7b84367417b2b2512b37
2021-02-06 04:42:36 +00:00
title: Catch Mixed Usage of Single and Double Quotes
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-09-07 16:09:54 +08:00
forumTopicId: 301188
2021-01-13 03:31:00 +01:00
dashedName: catch-mixed-usage-of-single-and-double-quotes
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
JavaScript allows the use of both single (`'` ) and double (`"` ) quotes to declare a string. Deciding which one to use generally comes down to personal preference, with some exceptions.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Having two choices is great when a string has contractions or another piece of text that's in quotes. Just be careful that you don't close the string too early, which causes a syntax error.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Here are some examples of mixing quotes:
2020-09-07 16:09:54 +08:00
```js
// These are correct:
const grouchoContraction = "I've had a perfectly wonderful evening, but this wasn't it.";
const quoteInString = "Groucho Marx once said 'Quote me as saying I was mis-quoted.'";
// This is incorrect:
const uhOhGroucho = 'I've had a perfectly wonderful evening, but this wasn't it.';
```
2021-02-06 04:42:36 +00:00
Of course, it is okay to use only one style of quotes. You can escape the quotes inside the string by using the backslash (<code>\\</code>) escape character:
2020-09-07 16:09:54 +08:00
```js
2021-02-06 04:42:36 +00:00
// Correct use of same quotes:
2020-09-07 16:09:54 +08:00
const allSameQuotes = 'I\'ve had a perfectly wonderful evening, but this wasn\'t it.';
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Fix the string so it either uses different quotes for the `href` value, or escape the existing ones. Keep the double quote marks around the entire string.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your code should fix the quotes around the `href` value "#Home " by either changing or escaping them.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/<a href=\s*?('|\\")#Home \1\s*?>/g));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
Your code should keep the double quotes around the entire string.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/"<p>.*?<\/p>";/g));
2018-10-10 18:03:03 -04:00
```
2020-08-13 17:24:35 +02:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
let innerHtml = "<p>Click here to <a href="#Home ">return home</a></p>";
console.log(innerHtml);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
let innerHtml = "<p>Click here to <a href=\"#Home \">return home</a></p>";
console.log(innerHtml);
```