2016-05-14 78 views

回答

5

使用negated character set

如果你没事保持下划线,你可以使用:

str.replace(/[^\w]/g, "-") // \w is the same as [A-Za-z0-9_] 

如果你想更换为好,那么你可以使用:

str.replace(/[^A-Za-z0-9]/g, "-") 

编辑:正如@JamesBuck所提到的,实际上有negated shorthands:​​与相同,所以如果你不需要更换下划线,您可以使用,致使:

str.replace(/\W/g, "-") 
+0

或者只是'\ W'为否定'\ w' –

+0

@JamesBuck接得好。 – timolawl

+0

完美。我需要第二个替代下划线。非常感谢 – tata

0
var str = "djdkasfh [email protected]@FA"; 
str.replace(/[^a-z0-9]+/gi, '-'); 

输出djdkasfh-da-S-FA

+0

感谢很多:) – tata

相关问题