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:
let 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:
'! ! ! x@x'
Exampe
In this example, the search pattern looks
like this: any letter from 'a'
to 'k'
between x
's:
let 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:
'! ! xmx x@x'
Exampe
In this example, the search pattern looks
like this: any letter from 'A'
to 'Z'
between x
's:
let 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:
'xax ! xcx x@x'
Exampe
In this example, the search pattern looks
like this: any digit from 0
to
9
between x
's:
let 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:
'xax ! ! ! x@x'
Exampe
In this example, the search pattern looks
like this: any digit from 3
to 7
between x
's:
let 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:
'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:
let 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:
'! ! ! ! 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:
let 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:
'! ! ! 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:
let 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:
'! ! ! ! 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:
let 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:
'xx ! ! ! x123x xa3x'
Exampe
Let's make it so that the number of letters can be zero:
let 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:
'! ! ! ! x123x xa3x'
Practical tasks
Given a string:
let 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.
Given a string:
let 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.
Given a string:
let 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.
Given a string:
let 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.
Given a string:
let 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.
Given a string:
let 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.
Given a string:
let 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.
Given a string:
let 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.