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

Categories

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

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

1 Answer

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...