Javascript 正则表达式 [任意数字]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4931825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:49:42  来源:igfitidea点击:

Regular expression [Any number]

javascriptjqueryregex

提问by ShaneKm

I need to test for "[any number]" in a string in javascript. how would i match it?

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

Oh, "[" and "]" also need to be matched.

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

so string like "[1]" or "[12345]" is a match.

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

Non match: "[23432" or "1]"

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

So for example:

例如:

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

I need to replace input fields name: "items[0].firstname" to "items[1].firstname" thanks

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

回答by aorcsik

UPDATE:for your updated question

更新:对于您更新的问题

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

Try this:

尝试这个:

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

Hope this helps!

希望这可以帮助!

回答by morja

if("123".search(/^\d+$/) >= 0){
   // its a number
}

回答by John Slegers

You can use the following function to find the biggest [number]in any string.

您可以使用以下函数查找[number]任何字符串中的最大值。

It returns the value of the biggest [number]as an Integer.

[number]以整数形式返回最大的值。

var biggestNumber = function(str) {
    var pattern = /\[([0-9]+)\]/g, match, biggest = 0;

    while ((match = pattern.exec(str)) !== null) {
        if (match.index === pattern.lastIndex) {
            pattern.lastIndex++;
        }
        match[1] = parseInt(match[1]);
        if(biggest < match[1]) {
            biggest = match[1];
        }
    }
    return biggest;
}


DEMO

演示

The following demo calculates the biggest number in your textarea every time you click the button.

每次单击按钮时,以下演示都会计算 textarea 中的最大数字。

It allows you to play around with the textarea and re-test the function with a different text.

它允许您使用 textarea 并使用不同的文本重新测试该功能。

var biggestNumber = function(str) {
    var pattern = /\[([0-9]+)\]/g, match, biggest = 0;

    while ((match = pattern.exec(str)) !== null) {
        if (match.index === pattern.lastIndex) {
            pattern.lastIndex++;
        }
        match[1] = parseInt(match[1]);
        if(biggest < match[1]) {
            biggest = match[1];
        }
    }
    return biggest;
}

document.getElementById("myButton").addEventListener("click", function() {
    alert(biggestNumber(document.getElementById("myTextArea").value));
});
<div>
    <textarea rows="6" cols="50" id="myTextArea">
this is a test [1] also this [2] is a test
and again [18] this is a test. 
items[14].items[29].firstname too is a test!
items[4].firstname too is a test!
    </textarea>
</div>

<div>
   <button id="myButton">Try me</button>
</div>

See also this Fiddle!

另见这个小提琴

回答by Nabab

var mask = /^\d+$/;
if ( myString.exec(mask) ){
   /* That's a number */
}