Aram Mkrtchyan Aram Mkrtchyan -4 years ago 104
Javascript Question

Removing all characters other than Armenian, ASCII and Russian letters and some special chars with JavaScript regex

I have some problems with regexp. I'm try to remove all special characters without "- _" it works but only with Latin letters. Can someone help me how to do that.

I need remove all without letters numbers and "- _".

for example

string = "asd;'\;['/\''сдфсдфявэ';щш;э'սդֆսդֆսդֆ«»խլխլ";

Answer Source

You may use

var s = "՞։՜asd;'\;['/\''сдфсдфявэ';щш;э'սդֆսդֆսդֆ«»խլխլ";
var ret = s.replace(/[^\wа-яёА-ЯЁ\u0561-\u0587\u0531-\u0556-]+/g, '');
document.body.innerHTML = ret;

The regex matches multiple occurrences (due to the g modifier) of 1 or more (due to the + quantifier) characters that are NOT:

  • \w - ASCII letters, digits or _
  • а-яёА-ЯЁ - Russian letters
  • \u0561-\u0587 - Armenian capital letters
  • \u0531-\u0556 - Armenian lowercase letters
  • - - a hyphen.

See this Unicode reference on Armenian letter ranges used in the pattern.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download