最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 21

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/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论