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
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.
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.
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.
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.
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!
if("123".search(/^\d+$/) >= 0){ // its a number }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With