frenchie
frenchie

Reputation: 51927

Replace slashes with dots in javascript regex

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.

Upvotes: 2

Views: 2591

Answers (4)

Robert I
Robert I

Reputation: 1527

Your expression should be /\//g

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

Upvotes: 3

ozziexsh
ozziexsh

Reputation: 138

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

  • / - start of the regex

  • [/] match characters inside square brackets

  • /g end of the regex

Regex resources:

Upvotes: 2

m1kael
m1kael

Reputation: 2851

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 \.

Upvotes: 1

gabesoft
gabesoft

Reputation: 1228

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

Upvotes: 1

Related Questions