3

Possible Duplicate:
JavaScript replace all / with \ in a string?

I have a date='13/12/2010' I want to replace this '/' to '_' or something else.

I read this But I do not know how can that applied for my case .

0

3 Answers 3

9

Use a global RegEx (g = global = replace all occurrences) for replace.

date = date.replace(/\//g, '_');

\/ is the escaped form of /. This is required, because otherwise the // will be interpreted as a comment. Have a look at the syntax highlighting:

date = date.replace(///g, '_');
Sign up to request clarification or add additional context in comments.

Comments

3

One easiest thing :)

var date='13/12/2010';
alert(date.split("/").join("_")); // alerts 13_12_2010

This method doesn't invoke regular expression engine and most efficient one

1 Comment

split / join is not usually more efficient than a single call to replace. Regular expressions are less expensive than you think.
1

You can try escaping the / character like this -

date.replace( /\//g,"_");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.