Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
iliakan committed Apr 20, 2019
1 parent 79f0463 commit 0697954
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions 9-regular-expressions/01-regexp-introduction/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ From here on the color scheme is:


````smart header="When to use `new RegExp`?"
Normally we use the short syntax `/.../`. But it does not allow any variable insertions, so we must know the exact regexp at the time of writing the code.
Normally we use the short syntax `/.../`. But it does not support variable insertions `${...}`.

On the other hand, `new RegExp` allows to construct a pattern dynamically from a string.
On the other hand, `new RegExp` allows to construct a pattern dynamically from a string, so it's more flexible.

So we can figure out what we need to search and create `new RegExp` from it:
Here's an example of a dynamically generated regexp:

```js run
let search = prompt("What you want to search?", "love");
let regexp = new RegExp(search);
let tag = prompt("Which tag you want to search?", "h2");
let regexp = new RegExp(`<${tag}>`);

// find whatever the user wants
alert( "I love JavaScript".search(regexp));
// finds <h2> by default
alert( "<h1> <h2> <h3>".search(regexp));
```
````
Expand Down

0 comments on commit 0697954

Please sign in to comment.