Character sets in regular expressions in JavaScript

The \d and \w character groups are not very flexible. Even such a simple task as to find all letters but not digits cannot be solved with them. For such problems, square brackets should be used, representing the operation 'OR'.

Square brackets replace one character, any of the ones listed inside. For example, like this: /x[abc]x/ - we say that the letters 'x' should be at the edges, and one character inside: or 'a', or 'b', or 'c'.

You can write repetition operators after square brackets. For example, like this: /x[abc]+x/ - we say that there can be any number of 'a', 'b' and 'c' characters inside x's - in any combination.

You can not only enumerate characters, but create groups of characters by writing a hyphen between two characters. For example, like this: [a-d] - we get all characters from 'a' to 'd'.

Let's look at examples.

Exampe

In this example, the search pattern looks like this: any letter from 'a' to 'z' between x's:

javascriptlet str = 'xax xbx xcx x@x';
let res = str.replace(/x[a-z]x/g, '!');

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

javascript'! ! ! x@x'

Exampe

In this example, the search pattern looks like this: any letter from 'a' to 'k' between x's:

javascriptlet str = 'xax xbx xmx x@x';
let res = str.replace(/x[a-k]x/g, '!');

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

javascript'! ! xmx x@x'

Exampe

In this example, the search pattern looks like this: any letter from 'A' to 'Z' between x's:

javascriptlet str = 'xax xBx xcx x@x';
let res = str.replace(/x[A-Z]x/g, '!');

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

javascript'xax ! xcx x@x'

Exampe

In this example, the search pattern looks like this: any digit from 0 to 9 between x's:

javascriptlet str = 'xax x1x x3x x5x x@x';
let res = str.replace(/x[0-9]x/g, '!');

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

javascript'xax ! ! ! x@x'

Exampe

In this example, the search pattern looks like this: any digit from 3 to 7 between x's:

javascriptlet str = 'xax x1x x3x x5x x@x';
let res = str.replace(/x[3-7]x/g, '!');

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

javascript'xax x1x ! ! x@x'

Exampe

In this example, the search pattern looks like this: any letter from 'a' to 'z' or a number from 1 to 9 between x's:

javascriptlet str = 'xax x1x x3x x5x x@x';
let res = str.replace(/x[a-z1-9]x/g, '!');

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

javascript'! ! ! ! x@x'

Exampe

In this example, the search pattern looks like this: any letter from 'a' to 'z' or a letter from 'A' to 'Z' between x's:

javascriptlet str = 'xax xBx xcx x5x x@x';
let res = str.replace(/x[a-zA-Z]x/g, '!');

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

javascript'! ! ! x5x x@x'

Exampe

In this example, the search pattern looks like this: any letter from 'a' to 'z' or digits 1, 2 between x's:

javascriptlet str = 'xax xbx x1x x2x x3x';
let res = str.replace(/x[a-z12]x/g, '!');

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

javascript'! ! ! ! x3x'

Exampe

In this example, the search pattern looks like this: letters from 'a' to 'z' in the amount of 1 or more between x's:

javascriptlet str = 'xx xabesx xaadx x123x xa3x';
let res = str.replace(/x[a-z]+x/g, '!');

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

javascript'xx ! ! ! x123x xa3x'

Exampe

Let's make it so that the number of letters can be zero:

javascriptlet str = 'xx xabesx xaadx x123x xa3x';
let res = str.replace(/x[a-z]*x/g, '!');

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

javascript'! ! ! ! x123x xa3x'

Practical tasks

Задание 1

Given a string:

javascriptlet str = 'aba aea aca aza axa';

Write a regex that matches the following pattern: the letters 'a' are at the edges, and the letter 'b', 'e' or 'x' is between them.

Задание 2

Given a string:

javascriptlet str = 'a1a a3a a7a a9a aba';

Write a regex that matches the following pattern: the letters 'a' are at the edges, and a digit from 3 to 6 is between them.

Задание 3

Given a string:

javascriptlet str = 'aba aea afa aha aga';

Write a regex that matches the following pattern: the letters 'a' are at the edges, and the letter from 'a' to 'g' is between them.

Задание 4

Given a string:

javascriptlet str = 'aba aea afa aha aga';

Write a regex that matches the following pattern: the letters 'a' are at the edges, and the letter from 'a' to 'f' and from 'j' to 'z' is between them.

Задание 5

Given a string:

javascriptlet str = 'aAa aea aEa aJa a3a';

Write a regex that matches the following pattern: the letters 'a' are at the edges, and the letter from 'a' to 'f' and from 'A' to 'D' is between them.

Задание 6

Given a string:

javascriptlet str = 'aAXa aeffa aGha aza ax23a a3sSa';

Write a regex that matches the following pattern: the letters 'a' are at the edges, and small Latin letters are between them, without affecting the others.

Задание 7

Given a string:

javascriptlet str = 'aAXa aeffa aGha aza ax23a a3sSa';

Write a regex that matches the following pattern: the letters 'a' are at the edges, and small and large Latin letters are between them, without affecting the others.

Задание 8

Given a string:

javascriptlet str = 'aAXa aeffa aGha aza ax23a a3sSa';

Write a regular expression that matches the following pattern: the letters 'a' are at the edges, and small Latin letters and digits are between them, without affecting the others.