I would like to replace all characters in a string myString with "p", except "o".
For example:
"fgyerfgehooefwfweeo"
should become
"pppppppppoopppppppo"
I tried:
myString.replaceAll('[^o]*/g', 'p')
I would like to replace all characters in a string myString with "p", except "o".
For example:
"fgyerfgehooefwfweeo"
should become
"pppppppppoopppppppo"
I tried:
myString.replaceAll('[^o]*/g', 'p')
XMehdi01
On
Simple way without using Regex:
which String.split() the string into chars and do Array.map() with condition on letter 'o' then finally Array.join() them back.
console.log("fgyerfgehooefwfweeo".split('').map(chr => chr !== 'o' ? 'p' : chr).join(''))
replace(orreplaceAll).*after the character class; otherwise, multiple consecutive characters that are not "o" will be collapsed into a single "p".