我需要在 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/