开发者

Parsing number from string

How do I 开发者_开发技巧split selected value, seperate the number from text in jQuery?

Example:

myselect contains c4

Just get only the number 4.

$('select#myselect').selectToUISlider({
      sliderOptions: {
        stop: function(e,ui) {
          var currentValue = $('#myselect').val();
          alert(currentValue);
          var val = currentValue.split('--)
          alert(val);

        }
      }
    });


You can use regex to pull only the numbers.

var value = "c4";
alert ( value.match(/\d+/g) );

edit: changed regex to /\d+/g to match numbers greater than one digit (thanks @Joseph!)


1) if it's always : 1 letter that followed by numbers you can do simple substring:

'c4'.substring(1); // 4 
'c45'.substring(1); // 45

2) you can also replace all non-numeric characters with regular expression:

'c45'.replace(/[^0-9]/g, ''); // 45
'abc123'.replace(/[^0-9]/g, ''); // 123


If you know that the prefix is always only one character long you could use this:

var val = currentValue.substr(1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜