Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created October 2, 2012 10:11
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 martinnormark/3817999 to your computer and use it in GitHub Desktop.
Save martinnormark/3817999 to your computer and use it in GitHub Desktop.
Trim string for array/collection paths
// In ASP.NET MVC, input fields are often named e.g. 'Products[5].Name'.
// To get rid of the 'Products[5]' part, use the following.
inputChanged: function (event) {
var regex = new RegExp(/^\w*\[\d\]\./),
$input = $(event.target),
inputName = $input.attr("name");
while (regex.test(inputName)) {
var matches = regex.exec(inputName);
inputName = inputName.replace(matches[0], "");
}
console.log(inputName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment