Modifiers for regex strings in JavaScript
Modifiers in regexes created with
RegExp should be passed as
the second parameter. Let's look
at an example. Let's say we have
the following string:
let str = 'abc def';
Let's apply the following regular expression with a modifier to this string:
let reg = /[a-z]+/g;
let res = str.match(reg);
Let's rewrite this regular expression
using RegExp:
let reg = new RegExp('[a-z]+', 'g');
let res = str.match(reg);
Practical tasks
Rewrite a regular expression as a string:
let str = '123 456 789';
let reg = /[0-9]+/g;
let res = str.replace(reg, '!');