Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  105] [ 7]  / answers: 1 / hits: 194645  / 14 Years ago, mon, february 14, 2011, 12:00:00

I want to remove numbers from a string:



questionText = 1 ding ?


I want to replace the number 1 number and the question mark ?. It can be any number. I tried the following non-working code.



questionText.replace(/[0-9]/g, '');

More From » regex

 Answers
67

Very close, try:



questionText = questionText.replace(/[0-9]/g, '');


replace doesn't work on the existing string, it returns a new one. If you want to use it, you need to keep it!

Similarly, you can use a new variable:



var withNoDigits = questionText.replace(/[0-9]/g, '');


One last trick to remove whole blocks of digits at once, but that one may go too far:



questionText = questionText.replace(/d+/g, '');

[#93745] Friday, February 11, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;