Replace slashes with dots in javascript regex - Stack Overflow

admin2025-04-22  0

I have a date and I want to replace all the slashes with dots. This is what I have but it's not working:

var TheDate = "3 / 29 / 2017";
TheDate = TheDate.replace(/'/'/g,'.');
alert(TheDate);

What's not right? There's a jsFiddle here to test.

I have a date and I want to replace all the slashes with dots. This is what I have but it's not working:

var TheDate = "3 / 29 / 2017";
TheDate = TheDate.replace(/'/'/g,'.');
alert(TheDate);

What's not right? There's a jsFiddle here to test.

Share Improve this question edited Jul 19, 2018 at 6:46 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 30, 2017 at 0:05 frenchiefrenchie 52.1k117 gold badges320 silver badges528 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

Your expression should be /\//g

var d = "3 / 29 / 2017";
d = d.replace(/\//g,'.');
document.body.append(d);

Replace /'/'/g with /[/]/g

  • / - start of the regex

  • [/] match characters inside square brackets

  • /g end of the regex

Regex resources:

  • http://regexr./
  • https://regexone./

Try this TheDate.replace(/\//g, '.')

var TheDate = "3 / 29 / 2017";
TheDate = TheDate.replace(/\//g,'.');     
alert(TheDate);

You need to escape the / character. You do it by prepending it with a \.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745276622a293909.html

最新回复(0)