Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all backslashes in Javascript

How to remove all backslash in a JavaScript string ?

var str = "one thing\\\'s for certain: power blackouts and surges can damage your equipment.";

I want output like

one thing's for certain: power blackouts and surges can damage your equipment.

Update:

I am scrapping data from page with help of JavaScript & displaying on fancy-box popup.

please help me on this

like image 327
user2046091 Avatar asked Apr 23 '13 13:04

user2046091


People also ask

How do I get rid of backslash in HTML?

Try: string. replace(/\\\//g, "/");

How do you escape a backslash in JavaScript?

The backslash() is an escape character in JavaScript. The backslash \ is reserved for use as an escape character in JavaScript. To escape the backslash in JavaScript use two backslashes.

How do I remove a strings slash in typescript?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.

What is JavaScript backslash?

The backslash ( \ ) is an escape character in Javascript (along with a lot of other C-like languages). This means that when Javascript encounters a backslash, it tries to escape the following character. For instance, \n is a newline character (rather than a backslash followed by the letter n).


2 Answers

Use a simple regex to solve it

str = str.replace(/\\/g, '')

Demo: Fiddle

like image 176
Arun P Johny Avatar answered Sep 22 '22 05:09

Arun P Johny


This is the answer according to the title as the title is "Remove all slashes in Javascript" not backslashes. So here is the code to remove all the slashes from a string in JavaScript.

str = '/mobiles-phones/.png';
str.replace(/\//g, "")//output would be "mobiles-phones.png";
like image 40
Safeer Ahmed Avatar answered Sep 21 '22 05:09

Safeer Ahmed