Replace this character class by the character itself

https://sonar-v2.common.cnxloyalty.com/

phoneNumber.replace(/^([\d]{1})([\d]{3})([\d]{3})([\d]+)$/, ‘$1-$2-$3-$4’);

This regex used for formatting phone number to 1-702-531-1500

Getting the issue for \d as ‘Replace this character class by the character itself.’

Hi,

The [] in regex is meant for matching any single character from a set, like [abc] will match either a, b, or c. Using [\d] is unnecessary since it’s a set with just one digit class; \d alone suffices. This is the cause of “Replace this character class by the character itself”.

Similarly, {1} is superfluous because regex assumes one occurrence by default.

Your regex could be simplified to /^(\d)(\d{3})(\d{3})(\d+)$/

1 Like