Answer the question
In order to leave comments, you need to log in
How to replace all characters except a specific one?
Let's say I have a string +7(\999) 999 99 99
How to replace all 9 in the string with _ , except for the one preceded by the \ character , and remove the character itself, it can be anywhere.
The output should be +7(9__) ___ __ __
PS: 7(\999) 999 99 99 => +7(9__) ___ __ __
Answer the question
In order to leave comments, you need to log in
str.replace(/(\\?)(\d?)/g, (m, s, n) => s || n !== '9' ? n : '_')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question