cambiar el nombre de las entradas con expresiones regulares

I have inputs with names like this

  <textarea type="text" name="featured[items][58252][hpsummary]" cols="50" class="limit20_120">asdfasdfasfasfdsadfasdfaasdfasfdasfasfasdf</textarea>

and I need to renumber the items I can loop through all the inputs no problem, but I am stuck on how to re number them so that the above would then then be 1 instead of 58252

  <textarea type="text" name="featured[items][1][hpsummary]" cols="50" class="limit20_120">asdfasdfasfasfdsadfasdfaasdfasfdasfasfasdf</textarea>

i can numerate just not sure how to parse it

I was thinking of just splitting the string but i'm not sure if I could use an regex so that I don't have to worry if there are more items on the end or if there is a better way

preguntado el 09 de marzo de 12 a las 15:03

I'll just leave this here. Again: stackoverflow.com/questions/1732348/… -

@Cfreak i'm not trying to parse the html just the name string -

Maybe use serialize() and post() to send the input names (and values) to the server, parse and re-create them there, then use the results of the post() to replace the inputs? -

@Cfreak while I was thinking of the same comment this question is only about the stuff dentro the name attribute, which is sufficiently regular... -

1 Respuestas

¿Algo como esto?

$('.limit20_120').each(function(i){
    this.name = this.name.replace(/\d+/, i+1);
});

Demostración de trabajo - http://jsfiddle.net/ShankarSangoli/4RUxS/

respondido 09 mar '12, 15:03

that would be fine but hpsummary could be anything and more may follow - mcgrailm

if there are number further down stream wouldn't that change them as well ? - mcgrailm

It's a little more verbose, but if you need to restrict the replacement, just add more of the name to the regex, i.e. this.name.replace(/^featured\[items\]\[\d+\]/, 'featured[items]['+(i+1)+']'); - Chris Pratt

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.