hi ,
i am trying to replace the '\' with '\\' in jquery.. but its throwing errors
ex: i have a user id test\sample then need to replace with test\\sample since while comparing '\' showing as special character
kindly help me on this
hi ,
i am trying to replace the '\' with '\\' in jquery.. but its throwing errors
ex: i have a user id test\sample then need to replace with test\\sample since while comparing '\' showing as special character
kindly help me on this
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mudzakkir TohaRank #3,303 · 435 reputationPosted Jun 19, 2023, 9:40 PM EDT
JavaScript Strings The replace() Method replace() searches a string for a value, and returns a new string with the specified value(s) replaced: Visit Microsoft! \ let text = document.getElementById("demo").innerHTML; document.getElementById("demo").innerHTML = text.replace("\\", "\\\\");I try this code and it is working
documentation here:
https://www.w3schools.com/jsref/jsref_replace.asp
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 20, 2023, 12:05 AM EDT
var userId = 'test\\sample'; var replacedUserId = userId.replace(/\\/g, '\\\\'); console.log(replacedUserId);Vishal YelveRank #178 · 13.6K reputationPosted Jun 20, 2023, 12:50 AM EDT
Try this
var myStr = 'test\sample';
var newStr = myStr.replace(/\/g, "\\");
console.log(newStr)
Deepak RawatRank #646 · 3.7K reputationPosted Jun 20, 2023, 1:09 AM EDT
To replace a single backslash
\with a double backslash\\in a string using jQuery, you can use thereplace()method. Here's an example:var userId = "test\\sample"; var replacedUserId = userId.replace(/\\/g, "\\\\"); console.log(replacedUserId); // Output: test\\sampleVishal JoshiRank #377 · 6.7K reputationPosted Jun 20, 2023, 1:21 AM EDT
Hello Murali,
Please try the below code to get the expected result.
var sourceStr = "test\sample"; var resultStr = myStr.replace(/\/g, "\\"); console.log(resultStr)Thanks
Murali PRank #6,159 · 275 reputationPosted Jun 20, 2023, 6:42 AM EDT
thanks to each and everyone for spending time and providing me solution.
this site is increasing confidence..