我正在尝试全部取代。与空间。
我已经完成了正则表达式
var voice = "I am student of …… School"
voice = voice.replace(/(~|`|!|@|#|$|%|^|&|\*|\(|\)|{|}|\[|\]|;|:|\"|'|<|,|\.|>|\?|\/|\\|\||-|_|\+|=)/g, "");
console.log(voice)Run Code Online (Sandbox Code Playgroud)
它返回 "I am a student of the .... university"
但我想要这样的字符串=> "I am a student of the university"
当您随后用一个空格替换多个空格时,您的代码起作用
var voice = "I am student of the .....University, not the …… School"
console.log(voice)
voice = voice.replace(/(~|`|!|@|#|$|%|^|&|\*|\(|\)|{|}|\[|\]|;|:|\"|'|<|,|\.|…|>|\?|\/|\\|\||-|_|\+|=)/g, "")
.replace(/ +/g," "); // or /\s+/g
console.log(voice)Run Code Online (Sandbox Code Playgroud)
如果您继续提出更多的标点符号,那么您将面临一个永无止境的问题:
所以也许这更好:
var voice = "I am student of the .....University, not the …… School"
console.log(voice)
voice = voice.replace(/\W/g, " ")
.replace(/ +/g," "); // or /\s+/g
console.log(voice)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |