Skip to content
Advertisement

Javascript: using the replace method on hex or dec character using the hex or dec value itself

I have a HTML table with a sorting function to sort in ascending or descending order by column. To show that I am using the down- and up- pointing small triangles with hex code x25BE; and x25B4; respectively.

The problem is that I cannot replace these hex characters using the replace method. I can only do that by using the character as follows: mystring.replace(‘▴’,”); but this is not possible because my javascript code is generated and that ▴ character cannot be used in the generating code.

It would be ok for me to use the decimal codes #9662; and #9652; , if that helps. See the code for my sortTable function for the expressions I tried, including the suggestions from this post: javascript replaceing special characters

<html>
<meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<head>
  <script type="text/javascript">
    function sortTable(id,n) {
      table = document.getElementById(id);
      //I am skipping the sorting here. 
      //The question is: how to replace hex or dec characters?
      var ths = table.getElementsByTagName('TH')
      for (i = 0; i < ths.length; i++) {
        //Not working
        ths[i].innerHTML = ths[i].innerHTML.replace(' ▾','');
        ths[i].innerHTML = ths[i].innerHTML.replace(' ▴','');
        //Also not working        
        //https://stackoverflow.com/questions/4347366/javascript-replaceing-special-characters
        ths[i].innerHTML = ths[i].innerHTML.replace(/x25BE/g,'');
        ths[i].innerHTML = ths[i].innerHTML.replace(/[xAExFC]/g,'');
        //This works but I cannot use it!
        //ths[i].innerHTML = ths[i].innerHTML.replace(/[▴▾]/g,'');
        //mimick switching down-pointing small triangle with up-pointing one  
        if (i == n) {
          ths[i].innerHTML = ths[i].innerHTML + ' ▴';
        }
      }
    }
  </script>
</head>
<body>
<table id="tableID">
  <tbody>
    <tr>
      <th onclick="sortTable('tableID',0)">Col1 ▾</th>
      <th onclick="sortTable('tableID',1)">Column 2 </th>
    </tr>
    <tr>
      <td>A</td>
      <td>100</td>
    </tr>
    <tr>
      <td>B</td>
      <td>20</td>
    </tr>
    <tr>
      <td>C</td>
      <td>50</td>
    </tr>
  </tbody>
</table>
</body>
</html>

Advertisement

Answer

As @Kelvin Sherlock wrote in a comment, using u25BE works: .replace(‘ u25BE;’,”);

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement