kernal lora kernal lora - 2 months ago 5
jQuery Question

How can I remove slashes(\\) from the string

I have a string like this.

""\"\\\"\\\\\\\"<img title='In Progress' src='img/In Progress.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Explore about Master Setting in TP' onclick='taskLink(13721,89,Explore about Master Setting in TP)'>Explore about Master Setting in TP</span>\\\\\\\"\\\"\"""


I trying to extract the image tag from the string like following way.
How can I remove the extra slashes.

<img title='In Progress' src='img/In Progress.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Explore about Master Setting in TP' onclick='taskLink(13721,89,Explore about Master Setting in TP)'>Explore about Master Setting in TP</span>


I need to remove slashes("\") from the string, please can anyone help me.
Thanks.

Update 1: I got origin for this cause.
But I don't understand why this is happening.
Problem is:

var vrTaskSubject= $("#lblTskSubject").text()


Above one giving the result like this:

""\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"<img title='Planned' src='img/Planned.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Format the Campus drive machine and set up the machine ready for campus' onclick='taskLink(14492,133,Format the Campus drive machine and set up the machine ready for campus,)'>Format the Campus drive machine and set up the machine ready for campus</span>\\\\\\\\\\\\\\\"\\\\\\\"\\\"\"""


Then As soon as this was stringified it was adding more slashes like this

vrTskSubject = JSON.stringify($("#lblTskSubject").text());


This is giving the results like this.

""\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"<img title='Planned' src='img/Planned.ico' class='clsstatusimginner'/><span class='clstaskidlink' title='Format the Campus drive machine and set up the machine ready for campus' onclick='taskLink(14492,133,Format the Campus drive machine and set up the machine ready for campus,)'>Format the Campus drive machine and set up the machine ready for campus</span>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\"\\\"\"""


Like this as many times the string is stringified that many slashes adding.
Please help me why stringify doing this. Is there reason behind this.
How can I fix this, Please need help

Answer

Try this :

myString.replace(/\\/g, "")

A fully working example is :

var myString = "aa ///\\\\\\";
myString.replace(/\\/g, "")
// Outputs : "aa ///"

The g flag will remove every instances of \ in your string. And we have to escape the pattern using \ else you would get an error.

EDIT 1 : After seeing with OP in chat (you can see the full conversation in comments below), here is the solution for HIS specific issue :

First, remove noisy chars. in the string coming from the server

taskSubject = sourceGrid[0].TaskName.replace(/#%&%#/g, "");

Then remove the backslashes and extra quotes using

myString.replace(/\\|"/g, "")
Comments