Michael Sanchez
Michael Sanchez

Reputation: 519

Replace character in javascript

I need replace a character in a string for a backslash,

example

var string = "hello world!"; string.replace("!","\");

help me please

thanks!!!!

Upvotes: 1

Views: 56

Answers (1)

anubhava
anubhava

Reputation: 786091

You can use:

string = string.replace(/!/g, '\\');

backslash needs to be escaped with another backslash and better to use regex /!/ in order to replace all the occurrences of !

Upvotes: 1

Related Questions