如何使用Javascript或Jquery增加字符串中的数字

use*_*208 10 javascript

我的textarea的id是字符串和这种格式

ID = '费舍尔[27] .MAN'

我想克隆textarea并增加数字并获取id,fisher[28].man并将其添加到现有的textarea.

有没有办法用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>");
Run Code Online (Sandbox Code Playgroud)

必须有更简单的克隆方法,获取索引号,拆分和前置.

我也尝试用regEx做到这一点

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

任何人都可以帮我吗?

Sta*_*arx 14

正确的正则表达式就是这样

/fisher\[\d+\].man/
Run Code Online (Sandbox Code Playgroud)

这是一个通过它提取id的方法.

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

类似地,可以使用相同的替换技术来获得递增的id:

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

两种用法的演示