简体   繁体   English

增加字符串中的最后一个数字

[英]Increment last number in string

given the following string: 给出以下字符串:

Backticks[0].Folders[0]

I need to increment the latter number. 我需要增加后一个数字。

I'm not an expert with Regexp replacements, I tried this but it also selects the bracket. 我不是Regexp替换的专家,我试过这个,但它也选择了支架。

href = href.replace(/\d+]$/gi, matchedNumber++);

Is it possible to do the selection, incremental and replacement in a one liner? 是否可以在一个衬管中进行选择,增量和更换?

可能在一个行做

'Ace[0].Folders[0]'.replace(/\d+(?=]$)/, function(i) { return parseInt(i) + 1; })

Use a positive lookahead: 使用积极的前瞻:

/\d(?=]$)/gi

This will make sure there is any character after then the end of string after the digit, without including it in the replace. 这将确保在数字之后的字符串结尾之后有任何字符,而不包括在替换中。

If you would like to increment it, you could use match and parseInt : 如果你想增加它,你可以使用matchparseInt

var href = this.href;
var re = /\d(?=.$)/gi
href = href.replace(re, parseInt(href.match(re))+1);

Here is a page where you can learn more about this. 这是一个页面 ,您可以在其中了解更多相关信息。

Here is a fiddle. 这是一个小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM