javascript - 正则表达式 [任意数字]

标签 javascript jquery regex

我需要在 javascript 的字符串中测试“[any number]”。我将如何匹配它?

哦,“[”和“]”也需要匹配。

所以像“[1]”或“[12345]”这样的字符串是匹配的。

不匹配:“[23432”或“1]”

例如:

$('.form .section .parent').find('input.text').each(function(index){
      $(this).attr("name", $(this).attr("name").replace("[current]", "['"+index+"']"));
});

我需要将输入字段名称:“items[0].firstname”替换为“items[1].firstname” 谢谢

最佳答案

更新:您更新的问题

variable.match(/\[[0-9]+\]/);

试试这个:

variable.match(/[0-9]+/);    // for unsigned integers
variable.match(/[-0-9]+/);   // for signed integers
variable.match(/[-.0-9]+/);  // for signed float numbers

希望这对您有所帮助!

关于javascript - 正则表达式 [任意数字],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4931825/

相关文章:

css - 什么是安全和不安全的 css 样式(我们会过滤掉什么,我们会允许什么)?

php - 如何在文章页面显示之前的3篇文章?

javascript - 未捕获的类型错误 : Cannot set property 'width' of undefined, javascript

c# - 无法在 Firefox 浏览器上使用 AjaxFileUpload 控件的 asp.net WebForm 中更新标签

javascript - jquery 找到最接近的类并增加值

python - 查找子字符串周围的单词

单击输入时,JavaScript 显示一个 div?

ajax - Jquery 滚动在 IE 上点击两次

jQuery - 检查容器 div 是否具有具有指定属性值的子项

python - 从字符串中提取数字的最 Pythonic 方法