Character groups within sets in JavaScript regex

Character groups \d, \D, \w, \W, \sā…/c ā†, \S inside [] will denote groups, that is, they will still remain commands.

Example

In this example, the search pattern looks like this: any digit or letter from 'a' to 'f' is between x's:

let str = 'xax xbx x1x x2x xhx x@x'; let res = str.replace(/x[\da-f]x/g, '!');

As a result, the following will be written to the variable:

'! ! ! ! xhx x@x'

Example

In this example, the search pattern looks like this: letter 'x', then a non-digit, a non-dot, and not a small Latin letter, then letter 'z':

let str = 'xaz x1z xAz x.z x@z'; let res = str.replace(/x[^\d.a-z]z/g, '!');

As a result, the following will be written to the variable:

'xaz x1z ! x.z !'

Practical tasks

Write a regex that will find strings according to the pattern: digit or dot 1 or more times.

Write a regex that finds strings according to the pattern: a non-digit and non-letter from 'a' to 'g' from 3 to 7 times.

© learn.itnots.ru