<!DOCTYPE html> <html> <head> </head> <body> <script type="text/javascript"> var x="2015-01-30-15"; var y="2015-01-30-15"; var z="2015-01-30-15"; //默认情况下只替换第一个 console.log(x.replace("-","/")) //如果要想替换多个或全部,请用for循环 for (var i=0; i<2; i++) { var y=y.replace("-","/") } console.log(y) //如果只替换全部,请考虑正则 console.log(z.replace(/-/g,"/")) </script> </body> </html>
[Web浏览器] "2015/01-30-15"
[Web浏览器] "2015/01/30-15"
[Web浏览器] "2015/01/30/15"
时间: 2024-10-12 11:59:17