Javascript, regex: Replace specified characters with space and matched character -


i trying use javascript replace() regex expression when matches characters like: .,!? replace matched character surrounded spaces. example string "hello?!?" become "hello ? ! ? ".

is there better way doing string.replace() each character wish replace?

i know can select on characters easy enough '/[!\?\.]/g', getting replace same character matched eluding me.

it's simple adding back-reference, so:

"hello?!?".replace(/([!?\,\.])/g, ' $1 '); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -