admin管理员组

文章数量:1024615

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

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

本文标签: regexRemove everything but numbers and spaces in JavaScriptStack Overflow