JavaScript – Remove strange characters from string

Tags: , , , ,



How do I remove these strange characters from a string?

I have been trying with regex, but with no success.

Before Remove:

RegExr was created by gskinner.com, and is proudly hosted by Media Temple.

*ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ
هۣۗہہهۣۗہہهۣۗہہهۣۗہہهۣۗہہهۣۗہهۣۗہہهۣۗہہهۣۗہہهۣۗہہهۣۗہہهۣۗہ ╚═════ঔৣ͜͡➳༒ঔৣ͜͡➳══════╝ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗۗ ۗۗ

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.

After Remove:

RegExr was created by gskinner.com, and is proudly hosted by Media Temple.



Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.

I already tried:

/[^\x00-\x7F]/gu
/([p{L}|p{N}|p{S}|p{M}p{P}])/gu
/[WD]/g

Answer

const dirty_string = 'your string';

const unwanted_chars_regex = /[^wds.&,]/g;
const clean_string = dirty_string.replace(unwanted_chars_regex, '');

Regex explanation:

[^] – group of NEGATIVE selection (whatever is in this group WILL NOT be selected)
w – Letter
d – Digit
s – Whitespace
. – Dot (Does not need to be escaped inside a group [])
& – Ampersand
, – Comma
g – global flag (matching all results and not just the first result)



Source: stackoverflow