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!
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!
Share Improve this question edited Nov 8, 2011 at 10:36 Nathan 1,4833 gold badges18 silver badges43 bronze badges asked Apr 14, 2011 at 12:16 haemsehaemse 4,2636 gold badges30 silver badges41 bronze badges 3- This is a little unclear about exactly what you want it to return. Do you want it to return individual numbers, and the "/" character? Or do you want it to just filter out any other character and return the string with only 0-9 and '/' present? – Groovetrain Commented Apr 14, 2011 at 12:21
- 2 case: filter out any other character and return the string with only 0-9 and '/' present! – haemse Commented Apr 14, 2011 at 12:22
- like in the description, dont look at the "^\d*\.?\d*$" ... maybe its just bullshit :-) – haemse Commented Apr 14, 2011 at 12:23
1 Answer
Reset to default 21This 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/