如何从字符串中替换all/with -

Vin*_*ini 0 javascript regex

我有一个字符串字段01/01/1986,我使用替换法更换所有发生的/-

var test= '01/01/1986';
test.replace('//g','-')
Run Code Online (Sandbox Code Playgroud)

但它没有给出欲望的结果.任何指针都会有所帮助.

Mar*_*yer 6

你只有几个问题:不要把正则表达式放在引号中.这将它变成一个字符串而不是一个正则表达式,并寻找该文字字符串.然后\/用来逃避/:

var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
Run Code Online (Sandbox Code Playgroud)