0

Illegal Arrow : escape(strNotes) : result -> "%1A"

This is the illegal Arrow : https://www.htmlsymbols.xyz/unicode/U+001A

I am not able to paste the strNotes but strNotes in system is replaced by an illegal arrow looking character which breaks the system.

Proper Arrow : escape('→') : results -> "%u2192"

But if I use the above proper arrow, it works well.

How to detect the Illegals arrow and special characters in Javascript and remove them from String?

Nagendra Singh
  • 577
  • 1
  • 7
  • 24

3 Answers3

0

You can use replace(/ /, ''), and replace them all.

Nikita
  • 36
  • 5
  • Use something like this ``` let specialChars = "!@#$^&%*()+=-[]\/{}|:<>?,."; for (let i = 0; i < specialChars.length; i++) { stringToReplace = stringToReplace.replace(new RegExp("\\" + specialChars[i], "gi"), ""); }``` – Nikita Apr 04 '20 at 07:23
  • This would have worked if I know what I am replacing, but in my case, I dont know that. – Nagendra Singh Apr 04 '20 at 07:24
  • You can leave only characters str.replace(/[^a-zA-Z ]/g, "") – Nikita Apr 04 '20 at 07:28
  • @NagendraSingh I thought you said the character was \x1A, so you would write `replace(/\x1A/g, '\u2192')` to replace it with a real arrow. – Mr Lister Apr 05 '20 at 18:08
  • Yeah actually I ran a loop and included all characters from `https://www.htmlsymbols.xyz/unicode/U+001A` and removed all the characters from `0 - 36` after converting each to unicode. and removed it from the string. – Nagendra Singh Apr 05 '20 at 18:10
0

You can replace ASCII characters

text.replace(/[^\ -~]+/g, '')

-~ is minus tilde - ~

QuentinUK
  • 2,997
  • 21
  • 20
0

I ended up doing this, thanks to @devio

    String.prototype.toUnicode = function () {
        var uni = [],
            i = this.length;
        while (i--) {
            uni[i] = this.charCodeAt(i);
        }
        return "&#" + uni.join(';&#') + ";";
    };

    let setOfIllegalCharacters = new Set();
    for (let i = 0; i < 32; i++) {
        setOfIllegalCharacters.add("&#" + i + ";");
    }


        // this iterates through the strNotes string and removes all illegal or bad characters.
        for (let i = 0; i < strNotes.length; i++) {
            if (setOfIllegalCharacters.has(strNotes[i].toUnicode())) {
                strNotes = strNotes.replace(strNotes[i], '');
            }
        }
Nagendra Singh
  • 577
  • 1
  • 7
  • 24