admin管理员组文章数量:1024581
I am trying to use javascript replace()
with a regex expression so that when it matches certain characters like: .,!?
it will replace the matched character with itself surrounded by spaces. For example the string "hello?!?"
will bee "hello ? ! ? "
.
Is there a better way than just doing a string.replace()
for each character I wish replace?
I know I can select on the characters easy enough with '/[!\?\.]/g'
, but getting it to replace it with the same character it matched with is eluding me.
I am trying to use javascript replace()
with a regex expression so that when it matches certain characters like: .,!?
it will replace the matched character with itself surrounded by spaces. For example the string "hello?!?"
will bee "hello ? ! ? "
.
Is there a better way than just doing a string.replace()
for each character I wish replace?
I know I can select on the characters easy enough with '/[!\?\.]/g'
, but getting it to replace it with the same character it matched with is eluding me.
-
Should there be one or two spaces between
?
and!
in your example? – t.niese Commented Jul 10, 2013 at 19:32 - Either way, after I split the sentence using / +/g which accounts for any number of spaces being between them to account for user error – Scott101 Commented Jul 10, 2013 at 19:35
- If the number of spaces does not matter then the answer of LeonardChallis is the one you are looking for. – t.niese Commented Jul 10, 2013 at 19:37
2 Answers
Reset to default 5It's as simple as adding a back-reference, like so:
"hello?!?".replace(/([!?\,\.])/g, ' $1 ');
If '/[!\?.]/g'
matches as a regex, just capture the group by surrounding it with ()'s
'/([!\?.])/g'
Then use the returned matched group to get the character you matched
I am trying to use javascript replace()
with a regex expression so that when it matches certain characters like: .,!?
it will replace the matched character with itself surrounded by spaces. For example the string "hello?!?"
will bee "hello ? ! ? "
.
Is there a better way than just doing a string.replace()
for each character I wish replace?
I know I can select on the characters easy enough with '/[!\?\.]/g'
, but getting it to replace it with the same character it matched with is eluding me.
I am trying to use javascript replace()
with a regex expression so that when it matches certain characters like: .,!?
it will replace the matched character with itself surrounded by spaces. For example the string "hello?!?"
will bee "hello ? ! ? "
.
Is there a better way than just doing a string.replace()
for each character I wish replace?
I know I can select on the characters easy enough with '/[!\?\.]/g'
, but getting it to replace it with the same character it matched with is eluding me.
-
Should there be one or two spaces between
?
and!
in your example? – t.niese Commented Jul 10, 2013 at 19:32 - Either way, after I split the sentence using / +/g which accounts for any number of spaces being between them to account for user error – Scott101 Commented Jul 10, 2013 at 19:35
- If the number of spaces does not matter then the answer of LeonardChallis is the one you are looking for. – t.niese Commented Jul 10, 2013 at 19:37
2 Answers
Reset to default 5It's as simple as adding a back-reference, like so:
"hello?!?".replace(/([!?\,\.])/g, ' $1 ');
If '/[!\?.]/g'
matches as a regex, just capture the group by surrounding it with ()'s
'/([!\?.])/g'
Then use the returned matched group to get the character you matched
本文标签: javascriptregex Replace specified characters with space and matched characterStack Overflow
版权声明:本文标题:Javascript, regex: Replace specified characters with space and matched character - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744946584a2125722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论