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
714 views
in Technique[技术] by (71.8m points)

javascript - regex that replace any chars (except number/, .)

I have a string. I need to parse it, replacing any chars (except numbers (0 to 9),, and ..

How can I do it with javascript?

Tried with :

string.replace(/[a-zA-Z]*/, "");

but seems it doesnt works. Also, I need ANY chars, not only a-Z (also ?, /, white space, and so on, expect, as said, , and .

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
string.replace(/[^d,.]+/g, "");

should do.

  1. [^d.,] means "any character except digit, comma or period", a negated character class.
  2. Use + instead of * or you'll have lots of empty matches replaced with empty strings.
  3. Use /g so all instances are replaced.

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

...