# Variable with a regex in JavaScript

Regular expressions can be placed into variables. Let's look at an example. Let's say we have the following string:

`let str = 'aaa bbb ccc';`

Let's say we have the following code with a regex:

`let res = str.replace(/\w+/, '!');`

Let's move the regular expression into a variable:

`let reg = /\w+/; let res = str.replace(reg, '!');`

Move the regex into a variable in the following code:

`let str = 'a aa aaa aaaa aaaa'; let res = str.replace(/a+/, '!');`
← →
↑
en ru