user3252359
user3252359

Reputation:

How to remove backslash from a string in JavaScript

I have a string like this:

<tr><td><span class=\'label label-info\'>Dialed</span></td><td>9804292145453</td><td>A Jana</td><td>0sec</td><td>6:18PM, Mar 24, 2014</td></tr>

I want to remove the backslash & want result like this :

<tr><td><span class='label label-info'>Dialed</span></td><td>9804292145453</td><td>A Jana</td><td>0sec</td><td>6:18PM, Mar 24, 2014</td></tr>

Please help.

Upvotes: 0

Views: 262

Answers (2)

OutOfSpaceHoneyBadger
OutOfSpaceHoneyBadger

Reputation: 1048

you can use RegExp to find backslash in JS string like this:

string.eplace(/\\\//g, "/");

demo

Upvotes: -1

T.J. Crowder
T.J. Crowder

Reputation: 1074266

You've said what you quoted above is a string, but it's unclear whether you mean this (shortened a bit):

var str = "<tr><td><span class=\'label label-info\'>Dialed...";

...where what you've quoted is what you literally have within quotes, or this (note the backslashes):

var str = "<tr><td><span class=\\'label label-info\\'>Dialed...";

...where what you've quoted is the actual content of the string, not part of a string literal.

The first one above doesn't have any backslashes in it, it has escaped ' characters. The second one has backslashes in it.

To remove the backslahes from the second one:

str = str.replace(/\\/g, "");

When you give a regular expression with the g flag to replace, it applies globally throughout the string. Backslashes have special meaning in regular expressions, and so I've had to escape the backslash (with another backslash, it's the escape character for regular expressions as well as strings). So in the above, I'm saying to replace all backslashes with an empty string.

Upvotes: 4

Related Questions