'Javascript String escape character
Hi I have javascript string like this:
let a = "\"2\" Kilogram".
I want it to look like this a = "2 Kilogram".
I have used unescape and str.replace but the response is like " "2" Kilogram".
Solution 1:[1]
You can simply replace the double quote like:
a = a.replace(/"/g, '')
Demo:
let a = "\"2\" Kilogram"
console.log("Before:", a)
a = a.replace(/"/g, '')
console.log("After:", a)
Solution 2:[2]
let a = "\"2\" Kilogram";
console.log(a.replace(/"/g, ''))
Solution 3:[3]
You can use regex to replace \" like below
let a = "\"2\" Kilogram"
a = a.replace(/\"/g, "")
console.log(a)
Hope this helps.
Solution 4:[4]
If you generelly want to remove all kind of special characters of the English language in a string and not only "" you may use:
let string2replace = "\"2\" Kilogram";
var desiredstring = string2replace.replace(/[^\w\s]/gi, '');
alert(desiredstring);
Solution 5:[5]
let a = "\"2\" Kilogram";
a = a.replace(/\"/g, "");
console.log(a);
Replacing using regex will help you here as a pro.
\" is to be replaced and g is used as global iterator which will return only once all possibilities have been replaced.
Solution 6:[6]
a = a.slice(0, 2)+a.slice(3, a.length)+'"';
Or just use single quotes so the inner double quotes are not interpolated when you set the value a in the first place ;)
let a ='"2 Kilogram";'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | palaѕн |
| Solution 2 | Always Helping |
| Solution 3 | Nithish |
| Solution 4 | SteffPoint |
| Solution 5 | |
| Solution 6 |
