Reputation: 51927
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
Reputation: 1527
Your expression should be /\//g
var d = "3 / 29 / 2017";
d = d.replace(/\//g,'.');
document.body.append(d);
Upvotes: 3
Reputation: 138
Replace /'/'/g
with /[/]/g
/
- start of the regex
[/]
match characters inside square brackets
/g
end of the regex
Regex resources:
Upvotes: 2
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