如何使用 Javascript 或 Jquery 增加字符串中的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9781245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to increment number in string using Javascript or Jquery
提问by user983208
The id of my textarea is string and of this format
我的 textarea 的 id 是字符串和这种格式
id='fisher[27].man'
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.
我想克隆 textarea 并增加数字并获取 id asfisher[28].man
并将其添加到现有的 textarea 中。
Is there a way to get this done easily with jquery?
有没有办法用 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
我也试过用 regEx 做到这一点
var existingIdNumber = parseInt(/fisher[(\d+)]/.exec(s)[1], 10);
Can anybody help me with this?
有人可以帮我解决这个问题吗?
回答by Starx
Correct regex would be this
正确的正则表达式是这样的
/fisher\[\d+\].man/
Here is a way through through which you would extract the the id.
这是一种提取 id 的方法。
id = text.replace(/fisher\[(\d+)\].man+/g,"");
//Now do whatever you want with the id
Similarly, Same replacement technique can be used to get an incremented id as:
同样,可以使用相同的替换技术来获取递增的 id,如下所示:
existingId = 'fisher[27].man';
newId = existingId .replace(/(\d+)+/g, function(match, number) {
return parseInt(number)+1;
});
console.log(newId);
Demo with both usage
两种用法的演示
回答by vol7ron
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} );
回答by rodamn
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.
非常干净和可读。
回答by kerzek
If your Id has this format
如果您的 ID 具有这种格式
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');
回答by nrabinowitz
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"
回答by Pithikos
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'
.
唯一的缺点是,如果我们有类似'test99'
.