Hyphen inside sets in JavaScript regex

The hyphen is also a special character inside [ ] (but not outside). If you need the hyphen itself as a regular character - then put it where it will not be interpreted as a group separator.

Why it's important: You can make a group of characters without yourself noticing it. For example, like this - '[:-@]' - you think you are choosing a colon, a hyphen and an AT symbol, but in fact you get a group of characters between : and @. This group includes the following characters: ? < = > : ;

Where are they from? From the table ASCII - the colon has a number less than the an AT - and the result is a group. That is, all groups are obtained according to the ASCII table (if desired, you can use this).

How to deal with this: put a hyphen character where it will definitely not be perceived as a group character, for example, at the beginning or at the end (i.e. after [ or before ]).

You can also escape the hyphen - then it will denote itself, regardless of position. For example, instead of [:-@] write [:\-@] - and there will be no more group, but there will be three characters - a colon, a hyphen and an AT @.

Example

In the following example, the search pattern is: digit 1, then a letter from 'a' to 'z', then digit 2:

let str = '1a2 1-2 1c2 1z2'; let res = str.replace(/1[a-z]2/g, '!');

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

'! 1-2 ! !'

Example

Let's now escape the hyphen. The resulting search pattern is: digit 1 followed by letter 'a' or hyphen or letter 'z' followed by digit 2:

let str = '1a2 1-2 1c2 1z2'; let res = str.replace(/1[a\-z]2/g, '!');

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

'! ! 1c2 !'

Example

You can just rearrange the hyphen without escaping it:

let str = '1a2 1-2 1c2 1z2'; let res = str.replace(/1[az-]2/g, '!');

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

'! ! 1c2 !'

Example

In the following example, the search pattern is: the first character is small letters or a hyphen '-', then two letters 'x':

let str = 'axx Axx -xx @xx'; let res = str.replace(/[a-z-]xx/g, '!');

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

'! Axx ! @xx'

Example

In the following example, the search pattern is: the first character is small, capital letters or a hyphen '-', then two letters 'x':

let str = 'axx Axx -xx @xx'; let res = str.replace(/[a-zA-Z-]xx/g, '!');

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

'! ! ! @xx'

Example

You can place a hyphen between two groups - there it definitely won't make one more group:

let str = 'axx 9xx -xx @xx'; let res = str.replace(/[a-z-0-9]xx/g, '!');

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

'! ! ! @xx'

Practical tasks

Given a string:

let str = 'xaz xBz xcz x-z x@z';

Find all strings with the following pattern: letter 'x', capital or small letter or hyphen, letter 'z'.

Given a string:

let str = 'xaz x$z x-z xcz x+z x%z x*z';

Find all strings with the following pattern: letter 'x', then either dollar, or hyphen or plus sign, then letter 'z'.

enru