Regex to remove letters, symbols except numbers

JavascriptRegexSymbols

Javascript Problem Overview


How can you remove letters, symbols such as ∞§¶•ªºº«≥≤÷ but leaving plain numbers 0-9, I want to be able to not allow letters or certain symbols in an input field but to leave numbers only.

Demo.

If you put any symbols like ¡ € # ¢ ∞ § ¶ • ª or else, it still does not remove it from the input field. How do you remove symbols too? The \w modifier does not work either.

Javascript Solutions


Solution 1 - Javascript

You can use \D which means non digits.

var removedText = self.val().replace(/\D+/g, '');

jsFiddle.

You could also use the HTML5 number input.

<input type="number" name="digit" />

jsFiddle.

Solution 2 - Javascript

Use /[^0-9.,]+/ if you want floats.

Solution 3 - Javascript

Simple:

var removedText = self.val().replace(/[^0-9]+/, '');

^ - means NOT

Solution 4 - Javascript

Try the following regex:

var removedText = self.val().replace(/[^0-9]/, '');

This will match every character that is not (^) in the interval 0-9.

Demo.

Solution 5 - Javascript

If you want to keep only numbers then use /[^0-9]+/ instead of /[^a-zA-Z]+/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMacMacView Question on Stackoverflow
Solution 1 - JavascriptalexView Answer on Stackoverflow
Solution 2 - JavascriptKlemen TusarView Answer on Stackoverflow
Solution 3 - JavascriptbezmaxView Answer on Stackoverflow
Solution 4 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 5 - JavascriptAziz ShaikhView Answer on Stackoverflow