Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 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 Reply

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...