I have this string:
var someString = "23/03/2012";
and want to replace all the "/" with "-".
I tried to do this:
someString.replace(///g, "-");
But it seems you cant have a forward slash /
in there.
Answer: Use the JavaScript replace() method You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.
A regular expression is used to replace all the forward slashes. As the forward slash (/) is special character in regular expressions, it has to be escaped with a backward slash (\).
A slash. A slash symbol '/' is not a special character, but in JavaScript it is used to open and close the regexp: /... pattern.../ , so we should escape it too.
Think of the forward slash as quotation marks for regular expressions. The slashes contain the expression but are not themselves part of the expression. (If you want to test for a forward slash, you have to escape it with a backwards slash.)
You need to escape your slash.
/\//g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With