0

I am doing some html encode in js.

Looks like that I don't want to encode letters and numbers.

export const encode = (str) => {
  if (!str || str === '') return '';
  var buf = []

  for (var i = str.length - 1; i >= 0; i--) {
    buf.unshift(['&#', str[i].charCodeAt(), ';'].join(''))
  }
  return buf.join('')
}

This is how I encode.

Now, if character is a english letter, or france or russian letter, i don't want it encoded. So I think I am left with this: encode anything that is not letter/number and also [a-Z] regex mightn't be enough, because what if it's a russian letter? I wouldn't want it encoded.

Any regex idea?

Nika Kurashvili
  • 6,006
  • 8
  • 57
  • 123
  • Maybe you could put it the other way around -- only encode a limited set of special characters, and leave everything else as-is? – vlumi Aug 16 '19 at 05:46
  • makes sense. any way you could help me with regex with special characters? – Nika Kurashvili Aug 16 '19 at 05:48
  • 1
    This one: https://stackoverflow.com/a/37668315/295783 – mplungjan Aug 16 '19 at 05:52
  • If your focus is to ignore special characters, another alternative might be to make the most of ASCII values. Something like ```var normalChar; var ascii = str.charCodeAt(i); if((ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122) ){normalChar = true;}else{normalChar = false;} var number; ascii = str.charCodeAt(i); if(ascii >= 48 && ascii <= 57){ number = true;}else{number = false;} ``` – Sean2014 Aug 16 '19 at 06:05
  • This is an ASCII table where you can see which ASCII value corresponds to which character or number. https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html – Sean2014 Aug 16 '19 at 06:07
  • In the code I showed you in the previous comment, what is being done is to check if the character in question is within the range of "number 0-9", "alphabets A-Z", or "alphabets a-z" – Sean2014 Aug 16 '19 at 06:08

0 Answers0