Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  124] [ 2]  / answers: 1 / hits: 120822  / 13 Years ago, thu, february 16, 2012, 12:00:00

I would like to replace all the characters other than 0-9 in a string, using Javascript.



Why would this regex not work ?



 a100.dfwe.replace(/([^0-9])+/i, )

More From » regex

 Answers
2

You need the /g modifier to replace every occurrence:



a100.dfwe.replace(/[^0-9]+/g, );


I also removed the redundant i modifier and the unused capturing sub-expression braces for you. As others have pointed out, you can also use D to shorten it even further:



a100.dfwe.replace(/D+/g, );

[#87428] Wednesday, February 15, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocioblancac

Total Points: 699
Total Questions: 96
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;