使用replace方法删除特殊符号和多余的空格,并用下划线替换。

31 浏览
0 Comments

使用replace方法删除特殊符号和多余的空格,并用下划线替换。

我想从一个字符串中移除所有特殊字符和空格,并用下划线替换。

字符串是

    var str = "hello world & hello universe";

我现在有这个代码,它只替换空格:

      str.replace(/\s/g, "_");

我得到的结果是hello_world_&_hello_universe,但我也想移除特殊符号。

我尝试了这个str.replace(/[^a-zA-Z0-9]\s/g, "_"),但没有起到作用。

0