Search code examples
javascriptjquerystring-parsing

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

        }
      }
    });

Solution

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