Parag Kadam
Parag Kadam

Reputation: 3850

Javascript replace character with backslash

How do I replace + with \+ using the replace method?

console.log("abc+pqr".replace("+","\+"));

This gives the original string abc+pqr but I want abc\+pqr. How do I get this?

Upvotes: 0

Views: 41

Answers (1)

Wais Kamal
Wais Kamal

Reputation: 6180

You need to escape the backslash:

console.log("abc+pqr".replace("+","\\+"));

Upvotes: 3

Related Questions