Sanitise weird unicode characters in JS [duplicate]


I do not know what to search on Google or how to ask you better than just showing the example.

Let’s say you have a comment section. And some user is posting this kind of comment:
(I will add a lot of fake spaces because this goes over the text; check it out in the dev tools as well)

fake space

fake space

fake space

Ą̶̨̧̡̡̢̧̢̡̢̡̛̛̛̛͖̬͚͓͍̭̗̘̬͙̘̣̼̬̩̖͖̦̬͎̹͖̞̪̼̱̰̝̬̹̼̲̩̼̟̰̥͈͙͍͕̪̼̥̭̲̫̳͓̦̞̰͔̻̳͉͕̥̻̝͕̜͓̗̲̺͇̰̘̻̫̬̥̤̤̰̖̮͈͎̠̬̣̘̙͕̖̦̬͎̣͖̖̝̮͇͈͖̩̮̦̤̳͇̯̤̟͍͍̻̞̠̬̣͇̯̩̞̤̳̳̟̩̦̘̫̘͕̖̺̤̯͈̯̻̬̅́̽͑͒̔̆̌̈́͑̓̄̈́́͒̓̾̋̈́͗͗̀͗̊̍̎̑͒̇̑̒̿͆̇͋̒͂̔̄̑̈͗̇́͒̑̈́̆̓̉͋̍̉̌͌̒̒̏̇̉̾̀̀̒̾̾̒̉͊̒̊̈̊̎́̈́̋́͐̈́̓̓̀̅̈́͋͑͋͆́́̋̂͗͑͋͒͗̋̑́͌̐̅̋̊̈́̑́̈͆̃̆̽͋̑̂̏̊́̒̓͐̓̅͗͆̑̽͋̆̐͊̈̍̐̓̔͆͆̂͒̀̾̑͌͌͒̀͆͐̓̇͂͒́͐̿͂̑̈́̆͛̉͛̐̇̑̀͗͘̚͘̕͘̕̕̚̕̕̕͘̕͘͜͠͝͝͝͝͝͝͝͝ͅͅͅͅn̶̢̨̧̢̡̨̡̢̡̡̨̢̨̧̨̢̧̨̢̡̛̛͎̜͍̦̞̟̜̘͚̝͖̪̫̟̗̩͔͓̖͕̗͍̣͍̥̠͔̪͕̼̣̫̜̖̮͍̹̳̤̣͍͇̟̘̤̬͙͚̦̫̤̞͓̥̼̬͇͖̖̤̠̙͉̘̲̘̲͎̪͈̺̳̩̹̩̫͓̩̼̝̦͙̻̙̱͎͕͙͎̗̯̥͔͉͓̺̞̻͉̗̤̣̳̞̼̖̜̱͎̲̳͙̺̟͎͎̬̳͉̮͓̠̮̦͙͓͈͚̟̯̦͉̝͕͕͇̤̩̘͇̖̙̩̯̥͖̘͈̲̭̦̪͍͈̰͍̦̝̖̺̗͊̿͑̂̓͐̏̃̋͂͛͐̈̑̾̋̓͋̉̂̈́̊̃̌̿̏̎͛̓̅̍͋̑̋̊̓̄̃̃́͐̄͗́̍̏̉̈́́̎͛̓̂̿̔̾̾̚̚̕̕̕͘̕̚͘͜͜͜͜͜͜͜͝͠͠͝ͅͅͅ ̸̨̨̧̢̡̡̡̨̧̨̧̡̛̛̛̛̞͓̼̻͉͎̺͚̥̹̪͇̳̘̫̰͍̘̼͙̦̹̫̯̱̺̮̪̼͓̝̞̻̘͎̜̱̬̰̣̰̠̗̫̥̥̙͙̥̩͇̩̥̱̹̪̜̟͓̰͇̹̲̲̣͓̹̖̘̫̙͍̙̪̳͚̫̯͕͕̹̫̼͈͖̦̗̗̘͔̩͇̯̠̻͓͈̤̲̱͚̝̪̣̖̣͎̜̱̺̜̗͖̪͕͉̞̙̝̯͖̭̰̳͎̖̩̾̍̿̿͋͗̑́̀̒̏̄͊̍̓̌̋̽̂̋̿̇̈͑̃̈́̀̋̆͌̇̏͗͐̌͗̂̀̽̑̑́̋͋͌̒̂̄͐̈́̀̿̂̽̓͊̉̈̄̇̉̎̈́̓̀̎̅̀̀̽͆́̓̆͊̄͛̓͂̏̈́̓̊̉̄͗̑̎̒͒͑͌̆͂̽̐̈́̒̉̓͊͒́̈́̾͊̏͛̾̔̀͐͆̋̓͊͊̒́̾̇̿͊̔̔͛̐̊͛͗̂̈́̀̏̇́͒̾̓́̇͒̀͛͌̄͒̾͌̾̿̿̉̑̈́̑̀̒̒́̅͆̑͘̕͘̕̚̕̕͘̚̕͘̕͝͠͝͝͝͝͠͝͝͝͝͝͝ͅͅͅͅě̸̛̛̛̛̘̝̩̪̩̱̠̲̳̟̦̊͑͋͋͗̎͒̒̊́͗̔͐̓͊̀̿̎͒̒́̊̋̉̎̄̈́̑̅̾́͛̍͗̉̂̊̊͑̑͌͐́̈́̍͛̌͒̏̀̄̑̄̊̓̃͑͛̄́͂̏̒͗͒̀̄̎̇̉̊̇̆̉̄̏̇̔̂̋͒͛̆̂̀̄̇̓̈́̅̎͐̓͐͌͒͑̐̾̋̀̉̂̇̽̇̽̃̇͛͆̈̈́̌͆̓̊͌̂̎̎̅͋͘͘̕̚͘̚̕͘̚̕͠͝͝͝͠͠͝͠y̷̡̢̢̡̨̛̛̛̛̛̛̛̛͔̥̠̦̺̘͓͔̭̩͈͈̫̝͖̙̺̗̟̠̖̰̩͚̪̹̲̥͉͇̫̪͔̼̗̮̞͇̭͇̜͚̬͇̦̖̻̬̻͍͕̼̯̆͛̂̄́͂̈̊̿̓͗͒̂͊͛̉̾͌́̉̊͂̔́͑͗̽̈́̔͂̿͒̔͒̓͗͐̑̆̅͆̀̊̾͗̒̔̀̉̔̈́͐̿͒͂̈͗̎̈́̋̔͛͒̉͊̌̓͐̇͒̔̌̾͋̊̋̀̄̃͑̐͗̑͛͋͑̋̈̀͗͗̀̉̾̀̄̈́̈̌͌͗̋̄͋̋̎̉͗̋͆̎͌̊͌̈́͑̆̈́̾̓͒̀̊͐̈̓̎̈̈́̈̔͂̈́̆̓͐̽̈́̀̊̂̏͌͋̏̀͑̽̿̃̏̀͋͋͌̈́͌͛̏͂͆̈̏́͐̏̓̌̊̈́̍̅̔̏͛̓̾̎̈́̈̋̏̓̄̀̿̆̉̈́̈̓͊͂̏̀͒͋͗̃̏̒̍̍̌̄͂̔̉̏̀̅̅̈̌͛̒͂̂͌̎̉͒̉̔͊̈́͗̍̆̌͆̈́̅̃̇͒͗͋̐̅͋̔͘̚̚͘̚̚̚͘̚̚͘͘̚̚̕͜͝͝͝͝͝͠͠͝͝͝͝ͅͅȩ̵̡̡̨̢̢̨̨̧̡̨̢̡̧̡̢̡̡̧̛̛̛̛̭̗̲͈̻͎͖̙̟̯͙̤͇͍̙̤̮̩̮͖̫͇̯̜͙͔͓̮̦̭̮̯̼̱̬̺͉̱̪̫̼͇̹̭̩͓̙̞̟͈̝͖̜͙̹̰̙̼̱̹̫͙̦̠̣͕̘̼̬͎̼̺̘̖͚̰̱̜̪̦̰̲̣͓͉͔̣̲͈͔͇̞̤̝̲̮̙͖͈̭̹̖̰̬̩̮̝̭͎̫͍͓̯̤̠̝͖̞̝̯̬̤̫̲͙̲̺̗̠͙̙̘̜̟̺͉̺̳̲̖̰̬̬̪̤͚̹̦̩̞͕̥͓̗̟̞͇̜͔̳̜̘͙͇̮̼͖͇̲̫̠̪̫̼̥͐͒̽͂͒̓̂̌̑̐̿̋̓̀̇̿̽́̍͗̈̋̀̐̊́͛͑̂́͛͆̈́͒́̍̀͗̓̍̔̉͌̈̓̈́̐̅͋̓̍̿͐͋̎̈́̅̊̌̅̆̅͗̀͒̌̎͋̆͑̈́̉̑͂̔̐͋̉͂͒̇̔̓͐͒͂͌̅̎̈͊̎͋́͐͆̏͑̚̚͘̚̚͜͝͝͠͝͝͝͝͝͝͝ͅͅļ̸̢̨̨̡̢̧̢̛̛̛̛̛̛̩̖̤̰̱̻̞̰͔͕̯͔͙̠͔̝̺͖̤̖̯̥̬̝͉͚̠̮̩̣̱̬̠̦̲͉̺̳͕̞͔̼̬̦̲͎̬̲͙̭̣͍̣͕͈̻͉̦̹̫͓̬̼̦̟̤̪̜͚̩͚͔̥̺̽̈́̈̋̓̍̀̈̂͐͐͋̎͂͌̈́̓͛̄͑̔͌̌̀̑͊̊̂̌̃͆̊̀͊͋̓̀̋̂̍̾͆̈͂͌͌́͌͗̓̀̂͗̀͗̽́̀̇͑̃͂̐̋̏͌̈́̀̔̏́̆̉̈̾̅̓̎̃̈́̈́͋͋̄̐̔́̇͑͐͑̿̏̉̓͆̽̀̅͒̊̔͗̀͒̈́͆͐̾̇̓͗͋́́̄̈́͛͊̌͌̅̔͊̓͊̽͛͆͑̊̔̍͊̀̽̂̄͛͐̅̒̽͌̐̂́̃̒̽̏̂͆͌̿̑͒̀͛̏̈́̈́͋͂̎̅̇̇͋̓͊͗͑͂̈̐̾̈̄̑̂̓̊͑͗͛̔̎̃̂͌̐̀̋̇̌͋̌̿̉̊͆̈́͑̉͋́̐̋̂̑͑̅̿͛͂̋̈́͂̎̎̊̇̿̄͋̑̽̓͗̋̿̐͗́̽̎̉̈̓͌̓̒͗̈̍̈̈̓̑̔̚̚͘͘̚̕͘̕̕̕͘̕̕͘̕͘͘͘̚͜͜͜͝͝͝͝͠͠͠͠͝͝͠͝͠͝͝͝͝͠͝͠ͅͅë̶̡̛̛̛̛̛̯̲̞̪̝̜̫͇̟͓͎̲̲͚̱̖͈̖̫̖̭́͋̆̓͗̅̾̀͗̓̈́͗̏͊͛̉̋̅̎̈̈́̅͂͒̑̉̒̉̀̇̐̄͒̃́̓͐̅̎̃̽͛́̑̽̈͋͋̎̔̂̑͋̎̃͋͋͒͌̐̌́́̔͒̏̓́͛̎̾̈́̒̑̇̎̌̔̅͌̇̽̄̃̂̽̅̌͛̈́̈́̐́̑̉̒̾̆͛̉͆̽̃̓̊̔̊̊͋̀͛͊̃̀̀͆̈̓̈́̏͌̊́̅̂̽̄̎̇͆̀͂̏͐̓̅́̎̂̂̈́̐͊́͐̈́͐͛̋̾̉̈́̽̌́̎̌̄̐̀͐̿̐͛̉͊̑̅̈͊̎̑̃́̿̋̈́̓̎̑͛̔͂̐̊̆͑̀̔̃̀͗̑̐̐̑͑͌̉̓̆̿͂͌̅̉̃͒͘̕̕͘̚̕̕̕̚̕̚͘̕̕͘͝͠͝͝͠͝͝͝͝͠͝͝ș̸̨̢̢̡̢̧̢̢̡̛̛̙̬͔̬͚̺̣̬̙͖͉̟̺̙͕̱͇͚̥̻̱̤̪̟̬̦̯̰̦̙̬̤̠̞̹̱͚̘̱̲̖̠̟͕̝͚̪̦͓͎̬̙͇̪̮̩͚̫͍̫̖̗͚͎̼̲̼̪̤̳̺͓̾̒̀̔̽̄̾̈̉̌͆̅̓̄̐̓͊̌̄͋̌̑͋͐͒̇͌͒̈̓̈͂̀̍̄̔̉͌̊́̀̓̀͗̈̎̄̑̓̓̀͊̊͛̾͘̕̚͜͜͝͝ͅͅs̵̢̨̧̢̨̢̧̢̢̨̛̻̰̺̯̻̪̦̳̲̣̤̗̩̞͔͚̦͈̜̣̰͓̤̭̩̺̫̤͍̩̪͖̩͓͔̱̳͕̩͕̦̲̼̜͉̟̠̗͎̻̱̙͈̫͎̠͎̦͓̫̩͉͉͍̯̗̲͎̖͕͔̠̣̠̣̯͚͙̥̞̻͓̱̦͔͕̣͇͔͚̳̼͔̘͎̙̣͈̱̜̰̯̮̥͖̘͓̤̘̣̙̗͉̘̣̥̞̺̜̰͇̖̦͉̞̠͎͇̼̙̣̘̣̳̞̦̞̫̹̱̙̱̱̦̹̺̫̖̦͍̖͇͙̘͕̬̬̙̐̉̈́̄͒̈́̈́͑̏̈́̆̋̍̍̏͊̈̒̓̔͛͑̒̍̒̆͗̔̽͒͋̉͗̀̍̇̄̒̎̈̀͆͊̄̈́̎̒̕̕̚̚͠͝͝͝͠͝͝͝͝ͅ ̸̡̧̡̡̛̛͔̱͈̮̤̭̤̺̙͚͓̘̬̣̗̳͍̞̝͎̬̼̳̫̞̗̄́̎͋͌̔̈́̌̾̎͋͑͒͗̈́́̃̈́̿͌̓̾͒̾̌͒́̄͊̔̀̀̔͗̽͛̾͗̿͌͒̐̏͂̂͂̍̆̔͂͗̓̾̓̒̎͑̆̉̚͘͘͘̚͜͝͝â̴̡̢̧̧̢̢̨̡̛̹̗͍̠̣̬̳̪̮͇͈͇̙̹̙̟͚̟͙͇͉̘͔̠͖͇̮̫̯̦̫͎͙̬̲̤͚̘̻̤̞͓̭̙̰̤̪̪̭͇̳̦̯̱̜̤̲͍̥͔̮͉͙͚̪̜͈̯̼̘̠̥͙̪̫̟͚͚̝̻̬͉̳̺͇̝͈͊̓̏͊͂͂̎̍̓̀̈́̈́̒͌͌̐̅̑͗̒̇̌̈́̈͂͑͘̚̕͘͜͠͝ͅb̵̢̨̨̡̢̨̡̧̨̢̡̧̧̢̨̢̡̢̢̧̨̧̧̧̧̢̡̢̨̢̛̛̛̛͈͈̻̼̘̬̯̘̥̱͎̦̜̣͕̱͓̬̹̟̰͚͔̦͇̩̖̜̟̻͎̖͇͇̗̱̖̰̳̥̤͕̪̞̩̝̺͚̝̘͈͎̣͍͚͙͉̩͕̝̱̻̜̺̞̹̩̪̯̗̩̗̘̠͉̱̖̪͕̠̻̤̟̥̯̯̲͓͙̜͚̜̻̣̖̰͔̹͍͎͙͈̞̹̗͈̜̗̪͙̭̫̼͕̖͓̣͓̞̱̞̣̤̩̥̱͖̪̞̲̞͓̖̫̳̻̫̮̠͉͍͖̹̮͖̤̳̱̩͕̙̙̦͚̬̰͉̯̦͔̲͖̻̞͍̲͍̞̰̦̻͙̘̯͙͉̱̥̝͚͙̙̯̺͍͈̠̟̟̰̪̣͈͔̹̟͍̮͙͕̯̹̎͐͋̈́̇̿͊̒̀̋̑̒͂̈̔͐̅̍̌̍͑̀̃̈́̓́͂͗̂̿́̽̈͑̀̃̎̈̾̈́̋̈̌́̅͐̄͊̎̀͊̂̓̓͂̉̽̾̓́͋̇̑̒̅̿͐͛̾͂̄̈̃̿̈̏̈́̔͆̈́̔͑͋͆̎̑́̓̾̓̔̀̑̌̌͂̓̎́̏̄̊̍͐̔̐̏̊̊̔̾́̓̀̊̌̂́̄̇̀̔̀̿̈̉͗͆̅̈́͛̾̽̾͌̓̇̀̅̀̎̊̋̆̽͐͊̋͆̉̓͒̆͆͌̒̿̔̑͒̓͆͌̋̎̓́͐̓͑̓̊̎͋̓̄́͛͆̏̾͑͒̏̍͆̌͆̈́̾̇̿̈́̄͋̓́́̿̑̌̈̐̊͐͘̚̚̚͘̕̚̕̚̕̕̚͜͜͜͜͜͜͠͝͝͝͠͠͠͠͠͝͝ͅͅͅͅơ̵̧̨̨̡̧̡̡̨̡̨̡̢̡̢̢̧̢̢̨̨̢̢̛̛̛̛̺̬̻͍͕͇̥̞͍͇̫͓̻̪̘͉̝͈̦̥̪̰͓̦̥̤̗̱̹̳͓͚͕̙͓̖̖̬͙̘̘͙̗̝͕̟͍͓̠̥̙̹̖̥̻̞̯̹̝̹̙̙͎̪̝̗͚̰͈̥̬͍̙͍̜̬͚͈̪̩͖̱͍̙̤̣̮̖͉̫͈̲͓̠̜̱͓̥͓̤̰̟̟̙̹̼͓̫͔̺̟̦͉̳̠̬͇͉̠̭͚͈̮̹̭̝̱̝͖̥̼̺͔͖̺͇̩̱̱̳͈̪͖̟͓̳̥͉̤̹̮̗̭̙͕̙̘̘͔̹̟͔̘̠͕̼͈̙̰̻͍̤͓͈̖̼͍͓̰̲͇͓̠͈̱̺̖̦̱͔̗̟̘͖͓̼̩̪͎̗̠̘͈̗̙͔̭̪̰̟͍̝̙̦̜̫̱̮͒̾͊̍̈́͆̒͌̓̐̌̑̒̾̏͂̉͆͋̅̾͋̂̊̊̓̑͛̈̌̎̄̆̃̌̊̅̃̋͌͒̂̋͂̆͒͒̈́̎̒̀̌̆̉̐̓̏̇̄͗́̄͐̈̏̀͊̃̿̂́̂́̌̒̉̋̃̂̾̈́͂͑͒̍̅̔̀̿̽̀̎͊̋́̓̊́͋̄͋͋͊̾͐̋̔̓̉̌̒͐̄̐̿̌̋̂͂̎̆̑͆̔̅͐̂̽̔͗́̒̄͗̓͊̋̈́̋͒̓͋͐̆͂̎̊͆̊̀̐͂̽̿̐͑͑͊̈́̌͆̐̐̈̈̈́̌͗̅̄̉̀̂͗̓̌̀́͆̀̊͂̓͒͌̃̌͊̅͑̀̈͊̈́͊̐̅̆̀̈́̌͗͌͘̚̕̕̚̚̚͘̕̕͘̕̚̚̚̚̚͘͘͜͜͜͜͜͜͝͝͝͠͝͝͠͠͝͝͠ͅͅͅͅ

fake space

fake space

fake space

fake space

How to detect this before the user makes a comment? Any regex or JS function?
But we also want to allow e.g. Chinese characters
印刷和排版行

Source: React – Stack Overflow

November 25, 2021
Category : News
Tags: javascript | reactjs | regex

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.