我怎样才能删除所有不是字母或数字的字符?

我有一个字符串:

var string = 'This - is my 4 String, and i - want remove all characters 49494 that are not letters or "numbers"';

我想转换成这样:
var string = 'This is my 4 String and i want remove all characters 49494 that are not letters or numbers'

这个有可能?

谢谢!!

最佳答案

您可以使用以下正则表达式:

[\W_]+

这个想法是为了与\W匹配一个非单词字符(那些不是A-Za-z0-9_的字符),并明确添加_(因为下划线被认为是单词字符)

Working demo
var str = 's - is my 4 String, and i - want remove all characters 49494 that are not letters or "numbers"';
var result = str.replace(/[\W_]+/g, ' ');

关于javascript - 删除字符串中不是字母或数字的所有字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30824525/

10-13 02:26