Skip to content

Instantly share code, notes, and snippets.

@jgarber623
Created December 1, 2011 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgarber623/1419329 to your computer and use it in GitHub Desktop.
Save jgarber623/1419329 to your computer and use it in GitHub Desktop.
An obtuse Regular Expression that'll replace indices in input @name and @id attributes in multi-model Rails forms
// RegExplanation: Match last occurrence of "_<string>_" and replace with "_<new_string>_"
some_string.replace( /^(.*)_.+_(.*?)$/, "$1_" + new_fields_index + "_$2" ),
// RegExplanation: Match last occurrence of "][<string>][" and replace with "][<new_string>]["
some_string.replace( /^(.*)\]\[.+\]\[(.*?)$/, "$1\]\[" + new_fields_index + "\]\[$2" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment