Regex remove all special characters except numbers?

前端 未结 5 1968
滥情空心
滥情空心 2021-01-31 09:09

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 ]/, \"\");
<         


        
5条回答
  •  耶瑟儿~
    2021-01-31 09:24

    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)

提交回复
热议问题