[英]Regex remove all special characters except numbers?
I would like to remove all special characters (except for numbers) from a string.我想从字符串中删除所有特殊字符(数字除外)。 I have been able to get this far我已经能够做到这一点
var name = name.replace(/[^a-zA-Z ]/, "");
but it seems that it is removing the first number and leaving all of the others.但似乎它正在删除第一个数字并留下所有其他数字。
For example:例如:
name = "collection1234"; //=> collection234
or或
name = "1234567"; //=> 234567
Use the global flag:使用全局标志:
var name = name.replace(/[^a-zA-Z ]/g, "");
^
If you don't want to remove numbers, add it to the class:如果您不想删除数字,请将其添加到类中:
var name = name.replace(/[^a-zA-Z0-9 ]/g, "");
要删除特殊字符,请尝试
var name = name.replace(/[!@#$%^&*]/g, "");
If you don't mind including the underscore as an allowed character, you could try simply:如果您不介意将下划线包含为允许的字符,您可以简单地尝试:
result = subject.replace(/\W+/g, "");
If the underscore must be excluded also, then如果还必须排除下划线,则
result = subject.replace(/[^A-Z0-9]+/ig, "");
(Note the case insensitive flag) (注意不区分大小写的标志)
to remove symbol use tag [ ]删除符号使用标签 [ ]
step:1步骤:1
[]
step 2:place what symbol u want to remove eg:@ like [@]第 2 步:放置您要删除的符号 eg:@ like [@]
[@]
step 3:第 3 步:
var name = name.replace(/[@]/g, "");
thats it就是这样
var name="ggggggg@fffff" var result = name.replace(/[@]/g, ""); console .log(result)
Extra Tips额外提示
To remove space (give one space into square bracket like []=>[ ])删除空格(在方括号中留一个空格,如 []=>[ ])
[@ ]
It Remove Everything (using except)它删除所有内容(使用除外)
[^place u dont want to remove]
eg:i remove everyting except alphabet (small and caps)例如:我删除除字母表之外的所有内容(小写和大写)
[^a-zA-Z ]
var name="ggggg33333@#$%^&**I(((**gg@fffff" var result = name.replace(/[^a-zA-Z]/g, ""); console .log(result)
This should work as well这也应该有效
text = 'the car? text = '车? was big and* red!'又大又*红!
newtext = re.sub( '[^a-z0-9]', ' ', text) newtext = re.sub( '[^a-z0-9]', ' ', text)
print(newtext)打印(新文本)
the car was big and red这辆车又大又红
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.