49
Views
1
Comments
javascript special chars

Hi All,

I want to escape characters in javascript,

['a',  'à','á','â','ä','æ','ã','å','ā'],        ['c',  'ç', 'ć', 'č'],        ['e',  'è','é','ê','ë','ē','ė','ę'],        ['i',  'ï','ï','í','ī','į','î'],        ['l',  'ł'],        ['n',  'ñ', 'ń'],        ['o',  'ô', 'ö', 'ò', 'ó', 'œ', 'ø', 'ō', 'õ' ],        ['s',  'ß', 'ś', 'š' ],        ['u',  'û', 'ü', 'ù', 'ú', 'ū'],        ['y',  'ÿ'],        ['z',  'ž', 'ź', 'ż']        ];

function escapeSpecialChars(str) {

         return str

         .replace(/&/g, "&")

         .replace(/"/g, '\\"')

         .replace(/

         .replace(/>/g, ">")

         .replace(/'/g, "'")

          .replace(/`/g, "`")

           

         

      }


for(var i =0; i<cols.length;i++){


    var str=escapeSpecialChars(cols[i]);

 

    var myElements = MyTable.querySelectorAll('[data-header=\''+str+'\']');

  I  have written the function above but want to esacpe all the columns which contains special chars .

Can anyone suggest how to include below chars as well

['a',  'à','á','â','ä','æ','ã','å','ā'],        ['c',  'ç', 'ć', 'č'],        ['e',  'è','é','ê','ë','ē','ė','ę'],        ['i',  'ï','ï','í','ī','į','î'],        ['l',  'ł'],        ['n',  'ñ', 'ń'],        ['o',  'ô', 'ö', 'ò', 'ó', 'œ', 'ø', 'ō', 'õ' ],        ['s',  'ß', 'ś', 'š' ],        ['u',  'û', 'ü', 'ù', 'ú', 'ū'],        ['y',  'ÿ'],        ['z',  'ž', 'ź', 'ż']        ];

to club in function.;

2023-01-05 17-39-49
Leonardo Pires
Champion

Hi.

You can use regular expression to remove all special characters, you can have a look at this example.

/[^\w\d]/g

Example Regex

Regards

special char regex.jpg
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.