I need some advice using the RegExp Object.
It should only return numbers and the character "/" from the variable val ... I'm not experienced in the RegExp Object - this is what I got so far:
var val = $('.gallerystatus input').val();
var regExpr = new RegExp("^\d*\.?\d*$");
$('.gallerystatus input').val( only 0-9 and "/" );
Thanks for any advice!
To remove all characters except numbers in javascript, call the replace() method, passing it a regular expression that matches all non-number characters and replace them with an empty string. The replace method returns a new string with some or all of the matches replaced.
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.
To replace or remove characters that don't match a regex, call the replace() method on the string passing it a regular expression that uses the caret ^ symbol, e.g. /[^a-z]+/ . The replace method will return new string where the not matching characters are replaced or removed. Copied!
This should do the trick
value = value.replace(/[^\/\d]/g,'');
The trick is the ^
symbol. When it's inside a []
character class, the ^
is a negation operator for the class.
So in this example, the []
class is matching every character except slash and digits.
See it in action here: http://jsfiddle.net/5gMNg/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With