简体   繁体   中英

Use javascript to convert special character back to HTML entity code

In one function of my site, the inner HTML of a div element is changed to include a right arrow. This symbol is included in the innerHTML of "myDiv" as ➞

In a separate function, I am trying to match the innerHTML of "myDiv" with ➞

function myFunction() {
//Many lines of code then
    var myText = document.getElementById("myDiv").innerHTML;
    alert(myText);
    if (myText.match(/➞/g)) { //also tried with /➞/g
         alert('Yep');
         myText = myText.replace(/➞/g, " to yield ");
    }
//Many more lines
}

When I alert myText as in the function above (before the if statement), the alerted output contains an actual right arrow ➞, not ➞ The problem is that even though ➞ is present in the code to produce the innerHTML of "myDiv" in the first place (in a separate function), in myFunction() ➞ does not seem to match, since the alert('Yep') is not called.

I looked up and in desperation tried decodeURIComponent() but I was pretty sure this was not going to work and it seemed not to. Can anyone help with this problem? Thanks

Check it against an actual right arrow, then.

function myFunction() {
//Many lines of code then
    var myText = document.getElementById("myDiv").innerHTML;
    alert(myText);
    if (myText.match(/➞/g)) { //also tried with /➞/g
         alert('Yep');
         myText = myText.replace(/➞/g, " to yield ");
    }
//Many more lines
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM