最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

regex - Remove everything but numbers and spaces in JavaScript - Stack Overflow

matteradmin6PV0评论

I have a regular expression that currently matches any non-numeric characters:

var.replace(/[^0-9a-zA-Z ]/,'');

I now need to keep the spaces as well as numbers, but I am having difficulty modifying this.

Any help?

I have a regular expression that currently matches any non-numeric characters:

var.replace(/[^0-9a-zA-Z ]/,'');

I now need to keep the spaces as well as numbers, but I am having difficulty modifying this.

Any help?

Share Improve this question asked Feb 13, 2013 at 16:02 JonJon 3,21411 gold badges41 silver badges60 bronze badges 8
  • 1 What you have doesn't keep spaces and numbers? – Explosion Pills Commented Feb 13, 2013 at 16:04
  • Your regex does not match "any non-numeric characters"; it doesn't match alphabetic characters. – Pointy Commented Feb 13, 2013 at 16:04
  • your regex matches none alpha-numeric characters .. not only numeric characters – Hussein Nazzal Commented Feb 13, 2013 at 16:06
  • @Rohit Jain - I have been trying things, that is why the regex is almost plete, but I apologise for not being the JavaScript guru you expect me to be. – Jon Commented Feb 13, 2013 at 16:07
  • [^0-9\s] this will do it .. this will match every thing that is not a number or space – Hussein Nazzal Commented Feb 13, 2013 at 16:08
 |  Show 3 more ments

1 Answer 1

Reset to default 6

here is a regular expression that will match every thing that is not a number or a space ..

[^0-9\s]

explanation hey regex engine :

1- match every thing .

2- ^ --> that's not

3- 0-9 --> a number

4- \s --> and a space

Post a comment

comment list (0)

  1. No comments so far