Reputation: 281
I have many inputs fields with this way of syntax
<input name="AF[c_condition][1][0]" />
<input name="AF[c_condition][1][1]" />
<input name="AF[c_condition][1][2]" />
<input name="AF[c_user][1]" />
how to get by RegularExpression the number in first brackets only?
Upvotes: 0
Views: 39
Reputation: 1934
Try the following regex:
Use capturing groups to fetch what's between the first two brackets.
I think match[1]
will get you the first capturing group. I'm not really proficient with JavaScript:
var mystring = "STRINGABOVE";
var myRegexp = /name="\w+\[\w+\]\[(\d+)\]/gm;
var match = myRegexp.exec(myString);
alert(match[1]);
demo here: https://regex101.com/r/jH4iW5/1
Upvotes: 0
Reputation: 3902
If your input string is myinput
, then you can use javascript's .match()
like this:
myinput.match(/<input name="AF\[c_\w+\]\[(\d+)\]/)
and then number would be stored in the first capture group, $1
.
The [
is preceded by \
because [
is a special character in regex.
The special \w
means "any word character". The special +
means "one or more times". The special \d
means "any digit". The parenthesis around it capture it into the first capture group.
As you can see, my match pattern only matches the beginning part of your HTML tags. You may choose to make your matches more robust than this example.
Upvotes: 1