如何在javascript中将字符串解析为int [英] how to parse string to int in javascript

查看:65
本文介绍了如何在javascript中将字符串解析为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从javascript中的字符串中获取整数,如何从

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

test1,stsfdf233,fdfk323,

test1 , stsfdf233, fdfk323,

有人告诉我从该字符串中获取整数的方法.

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

这是一条规则,即int始终位于字符串的后面.

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

如何获取字符串中最后一位的int

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

推荐答案

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

第一步是一个简单的正则表达式- \ d + $ 将匹配末尾的数字.
下一步,我们在之前匹配的 string 上使用 parseInt ,以获得正确的数字.

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.

这篇关于如何在javascript中将字符串解析为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆