Replace forward slash “/ ” character in JavaScript string?

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.

Answers:

Answer

You need to escape your slash.

/\//g
Answer

Try escaping the slash: someString.replace(/\//g, "-");

By the way - / is a (forward-)slash; \ is a backslash.

Answer

First of all, that's a forward slash. And no, you can't have any in regexes unless you escape them. To escape them, put a backslash (\) in front of it.

someString.replace(/\//g, "-");

Live example

Answer

Escape it: someString.replace(/\//g, "-");

Answer

Remove all forward slash occurrences with blank char in Javascript.

modelData = modelData.replace(/\//g, '');
Answer

You can just replace like this,

 var someString = "23/03/2012";
 someString.replace(/\//g, "-");

It works for me..

Answer

Just use the split - join approach:

my_string.split('/').join('replace_with_this')
Answer

Area.replace(new RegExp(/\//g), '-') replaces multiple forward slashes (/) with -

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.