且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-19 12:35:34

正确的正则表达式是这个

Correct regex would be this

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

这是一个通过它你将提取id。

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

做任何你想做的事情同样,相同的替换技术可以是用于获得增加的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);