用一个空格替换所有非字母数字字符、换行符和多个空格 [英] Replace all non Alpha Numeric characters, New Lines, and multiple White Space with one Space
问题描述
我正在寻找一个简洁的 RegEx 解决方案来替换
I'm looking for a neat RegEx solution to replace
- 所有非字母数字字符
- 所有换行符
- 空格的所有多个实例
一个空格
<小时>
对于那些在家玩耍的人(以下确实有效)
For those playing at home (the following does work)
text.replace(/[^a-z0-9]/gmi, " ").replace(/\s+/g, " ");
我的想法是 RegEx 可能足以在一个语句中实现这一点.我认为需要的组件是
My thinking is RegEx is probably powerful enough to achieve this in one statement. The components i think id need are
[^a-z0-9]
- 删除非字母数字字符\s+
- 匹配任何空格集合\r?\n|\r
- 匹配所有新行/gmi
- 全局、多行、不区分大小写
[^a-z0-9]
- to Remove non Alpha-Numeric characters\s+
- match any collections of spaces\r?\n|\r
- match all new line/gmi
- global, multi-line, case insensitive
但是,我似乎无法以正确的方式设置正则表达式的样式(以下不起作用)
However, i cant seem to style the regex in the right way (the following doesn't work)
text.replace(/[^a-z0-9]|\s+|\r?\n|\r/gmi, " ");
<小时>
输入
234&^%,Me,2 2013 1080p x264 5 1 BluRay
S01(*&asd 05
S1E5
1x05
1x5
<小时>
期望输出
234 Me 2 2013 1080p x264 5 1 BluRay S01 asd 05 S1E5 1x05 1x5
推荐答案
注意,\W
保留下划线.[^a-zA-Z0-9]
的简短等价物是 [\W_]
Be aware, that \W
leaves the underscore. A short equivalent for [^a-zA-Z0-9]
would be [\W_]
text.replace(/[\W_]+/g," ");
\W
是 shorthand \w
用于 [A-Za-z0-9_]
单词字符(包括下划线)
\W
is the negation of shorthand \w
for [A-Za-z0-9_]
word characters (including the underscore)
这篇关于用一个空格替换所有非字母数字字符、换行符和多个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!