Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegExpr advice needed - remove all chars from string except of numbers (0-9) and "/"

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!

like image 825
haemse Avatar asked Apr 14 '11 12:04

haemse


People also ask

How do I remove all characters from a string except numbers?

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.

How do I remove a specific character from a string in regex?

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]+", "")

What is ?: In regex?

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.

Can regex remove characters?

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!


1 Answers

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/

like image 111
Spudley Avatar answered Sep 22 '22 08:09

Spudley