admin管理员组文章数量:1180493
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?
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?
Share Improve this question edited Mar 20, 2012 at 6:31 Starx 78.9k50 gold badges186 silver badges265 bronze badges asked Mar 20, 2012 at 4:10 user983208user983208 2352 gold badges4 silver badges9 bronze badges6 Answers
Reset to default 19Correct 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
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} );
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.
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'
.
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');
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"
本文标签: How to increment number in string using Javascript or JqueryStack Overflow
版权声明:本文标题:How to increment number in string using Javascript or Jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738127194a2065078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论