61

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

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

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

Non match: "[23432" or "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

Greg
  • 481
  • 1
  • 5
  • 21
ShaneKm
  • 20,823
  • 43
  • 167
  • 296

5 Answers5

102

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
starball
  • 20,030
  • 7
  • 43
  • 238
aorcsik
  • 15,271
  • 5
  • 39
  • 49
  • I tried this $(this).attr("name", $(this).attr("name").replace("/\\[[0-9]+\\]/", "['"+index+"']")); but it doesn't work. am i doing something wrong? – ShaneKm Feb 08 '11 at 10:55
  • 1
    never mind. it does work: var name = $(this).attr("name"); var newname = $(this).attr("name").replace(name.match(/\\[[0-9]+\\]/), "["+index+"]"); – ShaneKm Feb 08 '11 at 11:04
  • `/[-.0-9]+/` matches just a period, which isn't really matching a number. – Ryan Shillington Jan 23 '20 at 00:00
7
if("123".search(/^\d+$/) >= 0){
   // its a number
}
morja
  • 8,297
  • 2
  • 39
  • 59
5

I always use the following regular expression to detect any kind of number in a string. Had no issues so far.

'(([\+\-]*\d*\.*\d+[eE])?([\+\-]*\d*\.*\d+))'

In detail:

'([\+\-]*\d*\.*\d+)'

to match a (non-)decimal number with(out) leading digits or sign

'([\+\-]*\d*\.*\d+[eE])?'

to match an exponential base before the number.

If there are brackets around required, you can add them inside or outside of the surrounding paranthesis:

'(\[([\+\-]*\d*\.*\d+[eE])?([\+\-]*\d*\.*\d+)\])'

In fact the surrounding paranthesis are not necessary, but i keep them to easier concatenate the expression with others.

LuettgeM
  • 133
  • 2
  • 4
0

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

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

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.

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

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!

John Slegers
  • 45,213
  • 22
  • 199
  • 169
0
var mask = /^\d+$/;
if ( myString.exec(mask) ){
   /* That's a number */
}
Nabab
  • 2,608
  • 1
  • 19
  • 32