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, '');
Answer from Kobi on Stack OverflowVery 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, '');
Strings are immutable, that's why questionText.replace(/[0-9]/g, ''); on it's own does work, but it doesn't change the questionText-string. You'll have to assign the result of the replacement to another String-variable or to questionText itself again.
var cleanedQuestionText = questionText.replace(/[0-9]/g, '');
or in 1 go (using \d+, see Kobi's answer):
questionText = ("1 ding ?").replace(/\d+/g,'');
and if you want to trim the leading (and trailing) space(s) while you're at it:
questionText = ("1 ding ?").replace(/\d+|^\s+|\s+$/g,'');
Strip all non-numeric characters from string in JavaScript - Stack Overflow
regex - How to remove all numbers and all text from string by using javascript replace? - Stack Overflow
Regex for removing numbers and punctuation from strings/removing extra spaces and tabs
jquery - Removing Numbers from a String using Javascript - Stack Overflow
What is the best free editor for JavaScript?
How do I run JavaScript locally vs in the browser?
Which JavaScript version should I target in 2026?
Use the string's .replace method with a regex of \D, which is a shorthand character class that matches all non-digits:
myString = myString.replace(/\D/g,'');
If you need this to leave the dot for float numbers, use this
var s = "-12345.50 โฌ".replace(/[^\d.-]/g, ''); // gives "-12345.50"
I'm currently trying to get all the words from a webpage and having a bit of trouble removing some of the unnecessary things on the webpage.
Say I'm trying to get all the words on the Reddit Wikipedia:
https://en.wikipedia.org/wiki/Reddit
To get the text, I'd do text=document.body.innerText.
I would do removeStuff =text.replace(/(?:\r\n|\r|\n)/g, ' ') to remove some of the new lines and tabs, but after a while, it starts showing up again. There's also some punctuation and numbers that I want removed, but not sure how to approach it.
\d matches any number, so you want to replace them with an empty string:
string.replace(/\d+/g, '')
I've used the + modifier here so that it will match all adjacent numbers in one go, and hence require less replacing. The g at the end is a flag which means "global" and it means that it will replace ALL matches it finds, not just the first one.
Just paste this into your address bar to try it out:
javascript:alert('abc123def456ghi'.replace(/\d+/g,''))
\d indicates a character in the range 0-9, and the + indicates one or more; so \d+ matches one or more digits. The g is necessary to indicate global matching, as opposed to quitting after the first match (the default behavior).
The simplest way is to use a regex:
var s = "ABCblahFoo$%^3l";
var letters = s.replace(/[^A-Za-z]+/g, '');
- the
[]represents a character (or a number of different possible characters) - the
^means all characters EXCEPT the ones defined in the brackets A-Zequals capital lettersa-zequals lowercase letters
So... Replace every character that is NOT a letter with the empty string.
jsFiddle Demo
the above answer will remove spaces from the string.
var str = "Hello^# World/";
str.replace(/[^a-zA-Z ]/g, "");
use this. Simply add space after A-Z in the regex.