When considering the patterns below:
"profile[foreclosure_defenses_attributes][0][some_text]"
"something[something_else_attributes][0][hello_attributes][0][other_stuff]"
It is possible to extract the last part by utilizing non-capturing groups:
var regex = /(?:\w+(\[\w+\]\[\d+\])+)(\[\w+\])/;
str = "profile[foreclosure_defenses_attributes][0][properties_attributes][0][other_stuff]";
match = regex.exec(str);
["profile[foreclosure_defenses_attributes][0][properties_attributes][0][other_stuff]", "[properties_attributes][0]", "[other_stuff]"]
Nevertheless, the goal is to obtain everything except for the final segment. In simpler terms, everything excluding [some_text] or [other_stuff].
The solution does not seem apparent using noncapturing groups. What alternative approach can be taken?