L
L
lookingfor22021-09-02 18:31:37
JavaScript
lookingfor2, 2021-09-02 18:31:37

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

1 answer(s)
A
Alexandroppolus, 2021-09-02
@lookingfor2

str.replace(/(\\?)(\d?)/g, (m, s, n) => s || n !== '9' ? n : '_')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question