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);

        }
      }
    });

Answers:

Answer

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!)

Answer

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
Answer

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

var val = currentValue.substr(1);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.