If I have a rails form that output the following HTML name attribute
"recipe[ingredients_attributes][1][quantities_attributes][0][measurement]"
How can I split this into chunks with jQuery, so I can manipulate the integers. I would like to avoid the use of a regular expression if possible.
I have a similar function for the rails form helper ID attributes which is as follows
function reindexQuantID(existing, parentIndex, index) {
var str = existing.split('_');
str[1] = parentIndex;
str[3] = index;
return str.join('_');
}
This one was easier because the characters are delimited by an _
The following uses regex:
var regex = new RegExp("\[[a-z0-9_]*\]", "g");
var array = existing.match(regex);
array[1] = "[42]";
array[3] = "[007]";
return array.join("");
# "recipe[ingredients_attributes][42][quantities_attributes][007][measurement]"
The array returned by the regex before manipulation is as follows:
["recipe[ingredients_attributes]", "[1]", "[quantities_attributes]", "[0]", "[measurement]"]
Please note that I'm not very experienced with regex, so there may be a better match, but the code works.
PS Just noticed Sparda's comment, that seems to do what you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.