正则替换特殊字符

使用正则替换文案中所有的特殊字符为 空格+特殊字符+空格,
如以下文案中每个特殊字符的前后加上空格

"A 【tragedy】 of (statistics)in most schools, is how ;dull it’s made Tea chers spend hours wading through derivations equa”ti“ons and theorems and "

需要替换的字符有以下这些:

()
 :
 ,
 -
 …
 !
 .
 《 》
< >
 –
? 
 ""
; 
/
[ ]
阅读 4.3k
2 个回答

点击查看

[():,-…!.《》<>–?";\/\[\]]
let reg = new RegExp("(;|:|\\\(|\\\))","g")
let str = "A 【tragedy】 of (statistics)in most schools, is how ;dull it’s made Tea chers spend hours wading through derivations equa”ti“ons and theorems and"
str.replace(reg, " $1 ")

自行把你要替换的符号加到正则表达式的括号里,记得转义

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题