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
Post a Comment