Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression [Any number]

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

like image 693
ShaneKm Avatar asked Feb 08 '11 10:02

ShaneKm


People also ask

How do you use numbers in regular expressions?

The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.

What is the regular expression for numbers only?

To check for all numbers in a field To get a string contains only numbers (0-9) we use a regular expression (/^[0-9]+$/) which allows only numbers. Next, the match() method of the string object is used to match the said regular expression against the input value.

How do I match a range of numbers in regex?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


2 Answers

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!

like image 165
aorcsik Avatar answered Sep 18 '22 13:09

aorcsik


if("123".search(/^\d+$/) >= 0){    // its a number } 
like image 23
morja Avatar answered Sep 17 '22 13:09

morja