Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

regex - JavaScript - Remove strange characters from string

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...