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:
javascriptlet str = 'abc def';
Let's apply the following regular expression with a modifier to this string:
javascriptlet reg = /[a-z]+/g;
let res = str.match(reg);
Let's rewrite this regular expression
using RegExp:
javascriptlet reg = new RegExp('[a-z]+', 'g');
let res = str.match(reg);
Practical tasks
Задание 1
Rewrite a regular expression as a string:
javascriptlet str = '123 456 789';
let reg = /[0-9]+/g;
let res = str.replace(reg, '!');