Skip to content

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

<meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
  <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        
        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 + ' ▴';
<table id="tableID">
      <th onclick="sortTable('tableID',0)">Col1 ▾</th>
      <th onclick="sortTable('tableID',1)">Column 2 </th>



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

2 People found this is helpful