0

Can anybody tell me how can i get a numeric value from a string containing integer value and characters? For example,I want to get 45 from

var str="adsd45";
3
  • 4
    Possible duplicate of about 10,000 other questions. Commented Oct 23, 2013 at 9:00
  • 1
    You mentioned "integer value and characters" can you provide an example? Commented Oct 23, 2013 at 9:01
  • 1
    I think that he mean: "abc123def" Commented Oct 23, 2013 at 9:02

4 Answers 4

1

If your string is ugly like "adsdsd45" you can use regex.

var s = 'adsdsd45';
var result = s.match(/([0-9]+)/g);

['45'] // the result, or empty array if not found
Sign up to request clarification or add additional context in comments.

Comments

0

You can use regular expression.

var regexp = /\d+/;
var str = "this is string and 989898";
alert (str.match(regexp));

Comments

0

Try this out,

var xText = "asdasd213123asd";
var xArray = xText.split("");
var xResult ="";

for(var i=0;i< xArray.length - 1; i++)
{

if(! isNan(xArray[i])) { xResult += xArray[i]; }

}

alert(+xResult);

Comments

0
var str = "4039";
var num = parseInt(str, 10);
//or:
var num2 = Number(str);
//or: (when string is empty or haven't any digits return 0 instead NaN)
var num3 = ~~str;

var strWithChars = "abc123def";
var num4 = Number(strWithChars.replace(/[^0-9]/,''));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.