I’m trying to replace the square root symbol which is written in html as “√” (√
).
I used the following line to replace it with a space but it does not change the String at all.
JavaScript
x
3
1
dig = document.getElementById("dig").innerHTML;
2
dig = dig.replace(/'√'/g, ' ');
3
console.log(dig);
JavaScript
1
1
1
<div id="dig">√25</div>
What am I doing wrong here?
Advertisement
Answer
This answer is valid if you are accessing the innerText
or innerHTML
atribute of an Element
.
JavaScript
1
4
1
let s = document.getElementById("sqrt")
2
console.log("Text", s.innerText)
3
console.log(s.innerText.replace(/√/g, ""))
4
console.log(s.innerHTML.replace(/√/g, ""))
JavaScript
1
1
1
<div id="sqrt"> √2 == 1.4142</div>
As you can see √
gets evaluated and it’s value is then √
instead of that. The javascript replace
method cannot understand that √
is &radic
so you can just replace √
.
Also there is no need to use regex, you can just do the same with "√"