Michal Olszowski
Michal Olszowski

Reputation: 805

Jquery: String convert to number

I have problem with string in Jquery. I have to convert for example '4444aaaa5555' string to '44445555'. I want to cut everything beside the numbers, is there a function for that ?

Upvotes: 0

Views: 373

Answers (3)

SoftwareNerd
SoftwareNerd

Reputation: 1905

You can do that with javascript using regex

   var number = yourstring.replace(/[^0-9]/g, '');

This will get rid of anything that's not [0-9]

if you want to make it as function

  function getNumber(param)
  {
      return   param.replace(/[^0-9]/g, '');
  }

you can acces this by

 var number =getNumber(yourstring)

Upvotes: 0

Hasan Alaca
Hasan Alaca

Reputation: 232

jQuery doesn't have strict rules for string and int. (You don't need a C# thinking)

For deleting the letters and keeping the numbers, you can try this:

http://jsfiddle.net/L5S7Q/

$(document).ready(function(){
  $("button").click(function(){
    var oldprice = $("#price").html();
    var newprice = oldprice.replace(/[^0-9\.]/g, '');
    $("#price").html(newprice);
  });
});

Upvotes: 0

Ashley Medway
Ashley Medway

Reputation: 7309

myString = myString.replace(/\D/g,'');

Upvotes: 5

Related Questions