Groups of characters in regular expressions in JavaScript

There are special commands that allow you to select entire groups of characters at once. The command \d denotes a digit from 0 to 9. The \w command denotes a digit, Latin letter, or underscore. The \s command denotes a space or space character: space, newline, tab. You can invert the meaning of the command by writing a capital letter: for example, if \d is a digit, then \D is non-digit.

Example

Let's find all the digits:

let str = '1 12 123'; let res = str.replace(/\d/g, '!');

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

'! !! !!!'

Example

The repetition operators treat the group commands as a single unit, i.e. grouping parentheses are not needed. In the following example, the search pattern looks like this: digit from 0 to 9 one or more times:

let str = '1 12 123 abc @@@'; let res = str.replace(/\d+/g, '!');

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

'! ! ! abc @@@'

Example

In the following example, the search pattern looks like this: anything one or more times, but not a digit from 0 to 9:

let str = '123abc3@@'; let res = str.replace(/\D+/g, '!');

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

'123!3!'

Example

In this example, the search pattern looks like this: space character once:

let str = '1 12 123 abc @@@'; let res = str.replace(/\s/g, '!');

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

'1!12!123!abc!@@@'

Example

In this example, the search pattern looks like this: NON-space one or more times. All substrings separated by spaces will be replaced with '!':

let str = '1 12 123 abc @@@'; let res = str.replace(/\S+/g, '!');

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

'! ! ! ! !'

Example

In this example, the search pattern looks like this: digit or letter one or more times . All substrings consisting of digits and letters will be replaced with '!':

let str = '1 12 123a Abc @@@'; let res = str.replace(/\w+/g, '!');

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

'! ! ! ! @@@'

Example

In this example, the search pattern looks like this: NON-digit and NON-letter one or more times. In our case, '@@@' and all spaces fall under this definition (after all, they are neither digits nor letters.). Please note that at the end there is one '!' - the string ' @@@' with a space in front was converted into it:

let str = '1 12 123 Abc @@@'; let res = str.replace(/\W+/g, '!');

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

'1!12!123!Abc!'

Practical tasks

Given a string:

let str = 'a1a a2a a3a a4a a5a aba aca';

Write a regex that finds strings with the letters 'a' at the edges and one digit between them.

Given a string:

let str = 'a1a a22a a333a a4444a a55555a aba aca';

Write a regex that finds strings with the letters 'a' at the edges and any number of digits between them.

Given a string:

let str = 'aa a1a a22a a333a a4444a a55555a aba aca';

Write a regex that will find strings with the letters 'a' at the edges, and any number of digits between them (including zero digits, that is, the string 'aa').

Given a string:

let str = 'avb a1b a2b a3b a4b a5b abb acb';

Write a regex that finds strings of the following form: the letters 'a' and 'b' are at the edges, and non-digit and non-spaceā…/r] between them.

Given a string:

let str = 'ave a#b a2b a$b a4b a5b a-b acb';

Write a regex that finds strings of the following form: the letters 'a' and 'b' are at the edges, and non-letter, non-digit, non-space between them.

Given a string:

let str = 'ave a#a a2a a$a a4a a5a a-a aca';

Write a regex that will replace all spaces with '!'.

enru