Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace forward slash "/ " character in JavaScript string?

Tags:

javascript

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.

like image 519
Mo. Avatar asked Mar 22 '12 13:03

Mo.


People also ask

How do you find and replace a character in a string in JavaScript?

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.

What function could you use to replace slashes?

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

Do you need to escape forward slash in JavaScript?

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.

What is a forward slash in JavaScript?

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


1 Answers

You need to escape your slash.

/\//g 
like image 192
Chris Sobolewski Avatar answered Sep 28 '22 14:09

Chris Sobolewski