18

i want int from string in javascript how i can get them from

test1 , stsfdf233, fdfk323,

are anyone show me the method to get the integer from this string.

it is a rule that int is always in the back of the string.

how i can get the int who was at last in my string

9 Answers 9

52
var s = 'abc123';
var number = s.match(/\d+$/);
number = parseInt(number, 10);

The first step is a simple regular expression - \d+$ will match the digits near the end.
On the next step, we use parseInt on the string we've matched before, to get a proper number.

Sign up to request clarification or add additional context in comments.

13 Comments

@Chinmayee: "it is a rule that int is always in the back of the string"
@elusive: It works because the first step of parseInt is to call ToString on the input, in this case an array, and then it is a string. See section 15.1.2.2 in ECMA-262 specification.
I would also note that in JavaScript 1 == [1] is true, and even [[[[2]]]] == 2 - JavaScript also treats a single-element array as the element. I guess JavaScript is a sloppy language :)
@Kobi: "I guess JavaScript is a sloppy language" I disagree. There's a lot of sloppy JavaScript code out there, but the language isn't sloppy.
Given the reference @some made to the spec, I'm not certain this would be a bad (or sloppy) solution (unless there's some known flaw in a relevant js implementation).
|
3

You can use a regex to extract the numbers in the string via String#match, and convert each of them to a number via parseInt:

var str, matches, index, num;

str = "test123and456";
matches = str.match(/\d+/g);
for (index = 0; index < matches.length; ++index) {
    num = parseInt(matches[index], 10);
    display("Digit series #" + index + " converts to " + num);
}

Live Example

If the numbers really occur only at the ends of the strings or you just want to convert the first set of digits you find, you can simplify a bit:

var str, matches, num;

str = "test123";
matches = str.match(/\d+/);
if (matches) {
    num = parseInt(matches[0], 10);
    display("Found match, converts to: " + num);
}
else {
    display("No digits found");
}

Live example

If you want to ignore digits that aren't at the end, add $ to the end of the regex:

matches = str.match(/\d+$/);

Live example

3 Comments

That's /\d+$/. Cool if, by the way :)
@Kobi: Thanks for catching the $ thing, I caught it in the example but hadn't realized I'd typed it in the text (I normally copy from the example to the text, not the other way around).
+1 An if(matches) I'm certain will be a faster test than relying on parseInt() to do it for you.
2
var str = "stsfdf233";
var num = parseInt(str.replace(/\D/g, ''), 10);

2 Comments

Note : this solution works even if numbers are not at the end of string (also for string like ab12c34)
@fcalderan: Yeah, but almost certainly not the way it's supposed to. (Your "ab12c34" example will return the number 1,234. Desired result? I doubt it.)
1
var match = "stsfdf233".match(/\d+$/);
var result = 0; // default value
if(match != null) {
    result = parseInt(match[0], 10); 
}

Comments

1

Yet another alternative, this time without any replace or Regular Expression, just one simple loop:

function ExtractInteger(sValue)
{
    var sDigits = "";
    for (var i = sValue.length - 1; i >= 0; i--)
    {
        var c = sValue.charAt(i);
        if (c < "0" || c > "9")
            break;
        sDigits = c + sDigits;
    }
    return (sDigits.length > 0) ? parseInt(sDigits, 10) : NaN;
}

Usage example:

var s = "stsfdf233";
var n = ExtractInteger(s);
alert(n);

1 Comment

One small comment - you want to call parseInt(sDigits, 10), try abc012, and then abc09.
0

This might help you

var str = 'abc123';
var number = str.match(/\d/g).join("");

Comments

0

Use my extension to String class :

String.prototype.toInt=function(){
    return parseInt(this.replace(/\D/g, ''),10);
}

Then :

"ddfdsf121iu".toInt();

Will return an integer : 121

Comments

0

First positive or negative number:

"foo-22bar11".match(/-?\d+/); // -22

3 Comments

Can you explain your answer?
this is wrong as it doesn't answer but shouldn't be deleted. It has value. It finds the first
OP did not ask for all occurences, neither did i want that. And none of the given answers take into account negative numbers and that is what i needed so i figured this out.
-2
javascript:alert('stsfdf233'.match(/\d+$/)[0]) 

Global.parseInt with radix is overkill here, regexp extracted decimal digits already and rigth trimmed string

Comments

Your Answer

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