12

The id of my textarea is string and of this format

id='fisher[27].man'

I would like to clone the textarea and increment the number and get the id as fisher[28].man and prepend this to the existing textarea.

Is there a way to get this done easily with jquery?

var existingId = $("#at textarea:last").attr('id');
var newCloned = lastTextArea.clone();
var newId = newCloned.attr('id');
//add the index number after spliting
//prepend the new one to 
newCloned.prepend("<tr><td>" + newCloned + "</td></tr>");

There has to be easier way to clone, get index number, split and prepend.

I'm have also tried to do this with regEx

var existingIdNumber = parseInt(/fisher[(\d+)]/.exec(s)[1], 10);

Can anybody help me with this?

Starx
  • 77,474
  • 47
  • 185
  • 261
user983208
  • 235
  • 2
  • 4
  • 9

6 Answers6

19

Correct regex would be this

/fisher\[\d+\].man/

Here is a way through through which you would extract the the id.

id = text.replace(/fisher\[(\d+)\].man+/g,"$1");
//Now do whatever you want with the id

Similarly, Same replacement technique can be used to get an incremented id as:

existingId = 'fisher[27].man';
newId = existingId .replace(/(\d+)+/g, function(match, number) {
       return parseInt(number)+1;
});
console.log(newId);

Demo with both usage

Starx
  • 77,474
  • 47
  • 185
  • 261
  • @vol7ron, Thanks you for the suggestion, hope you will like the edit. – Starx Mar 20 '12 at 15:49
  • awesome, removed my comment :) – vol7ron Mar 20 '12 at 17:18
  • @Starx, thank you for you great answer and my question is if it is possible to increment number except number with special character e.g. existingId = 'fisher[27][ $27].man'; I would like increment [27] only, not [$27]. Regards Jan – JanZitniak Jun 06 '20 at 06:01
  • @JanZitniak One way is [this](http://jsfiddle.net/Starx/stXVu/134/) – Starx Jun 24 '20 at 10:04
3

Another easy solution from this question's accepted answer:

'url1'.replace(/\d+$/, function(n){ return ++n }); // "url2"
'url54'.replace(/\d+$/, function(n){ return ++n }); // "url55"

Very clean and readable.

Community
  • 1
  • 1
rodamn
  • 2,191
  • 19
  • 24
  • 1
    To fully solve request from original post, need to remove $ from regular expression so the number doesn't need to be at the end of string. – rodamn Sep 19 '15 at 05:10
3

jsFiddle Demo

No need for all the extra code. Change this line:

var newId = newCloned.attr('id');

To:

var newId  = newCloned.attr('id').replace( /(\d+)/, function(){return arguments[1]*1+1} );
vol7ron
  • 40,809
  • 21
  • 119
  • 172
2

I am amazed that there is not any good implementation for this simple thing. All of the examples forget the case of having zeroes before the number. The code below should take care of that.

// 'item001' => 'item002'
function increment_alphanumeric_str(str){
    var numeric = str.match(/\d+$/)[0];
    var prefix = str.split(numeric)[0];

    function increment_string_num(str){
        var inc = String(parseInt(str)+1);
        return str.slice(0, str.length-inc.length)+inc;
    }

    return prefix+increment_string_num(numeric);
}

Example:

> increment_alphanumeric_str('test09')
'test10'

Only drawback is that this will break if we have something like 'test99'.

Pithikos
  • 18,827
  • 15
  • 113
  • 136
1

If your Id has this format

id='fisher[27].man'

You can do something like this

   var startIndex = newId.indexOf('[');
   var endIndex = newId.indexOf(']');
   var number = parseInt(newId.substr(startIndex + 1, endIndex - startIndex - 1));
   var incrementedId = number + 1;  // 28

where

var newId = newCloned.attr('id');
kerzek
  • 490
  • 1
  • 5
  • 15
0

You can do this pretty easily with a regex replacement:

var id = "fisher[27].man";
id = id.replace(/\[(\d+)\]/, function(match, number) {
    return '[' + (parseInt(number, 10) + 1) + ']';
});
// id is now "fisher[28].man"
nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
  • What if you have `fisher[07].man`? It gives back `fisher[8].man` – Pithikos Dec 29 '15 at 16:57
  • a) Zero-padding didn't seem to be an element of the OP's use case. b) If you think this is a concern in my implementation, check out the accepted answer by @Starx, which omits the radix argument from `parseInt` and therefore [risks parsing in base 8 for 0-padded numbers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt). – nrabinowitz Dec 30 '15 at 03:47