0

I need your help to create a regex pattern to extract a character from the value of the attribute "name" of an html input.

This is because, with a button I clone dynamically a "select" and some input text, so I need to keep the same syntax of their attribute "name" value.

Example:

<input id="search-words02" type="text" name="words[1][element]">
<select id="search-words03" title="Auteur" name="words[1][target]">
  <option value="ALL">hello</option>
</select>

So I have words[1][element] and words[1][target]

And to submit the form in a json format with all fields dynamically cloned, I need to increment the words[1][element] and have words[2][element] or words[3][element].

So my json could be :

{
        "words":        {
            "0":
            {
                "element":"",
                "target":""
            },
            "1":
            {
                "element":"2",
                "target":"GES"
            }
            "2":
            {
                "element":"3",
                "target":"XXX"
            }
}

So I guess my pattern should take and replace the character in the first tab, but I have no idea how the regex should be to do that..

Anyone can help me to build this regex and tell me how I can replace the found term ?

nhahtdh
  • 52,949
  • 15
  • 113
  • 149
malak
  • 27
  • 5

1 Answers1

0

If there's only one number in your name attribute, you can easily extract it with "\d" like below

var w = "words[1][element]";
var num = parseInt(/\d+/.exec(w)) + 1;
w = w.replace(/\d+/, num);    
user3292788
  • 327
  • 3
  • 14
  • Thanks ! This solution works, i will adjust the regex if the name change, but for now this as you said, only one number. – malak Nov 24 '14 at 14:51