在javascript中解析字符串中的Int [英] Parsing an Int from a string in javascript

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

问题描述

在javascript中,从以字母开头的字符串(例如test [123])解析INT的最佳方法是什么?我需要一些适合下面例子的东西。

In javascript, what is the best way to parse an INT from a string which starts with a letter (such as "test[123]")? I need something that will work for the example below.

我的JS:

$(document).ready(function() {

    $(':input').change( function() {
        id = parseInt($(this).attr("id"));  //fails for the data below
        alert(id);
    });
}

我生成的HTML:

<select id="attribute[123]">
<!-- various options -->
</select>
<select id="attribute[456]">
<!-- various options -->
</select>

谢谢你好!

推荐答案

您可以使用正则表达式来匹配数字:

You could use a regular expression to match the number:

$(this).attr("id").match(/\d+/)

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

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