Increase last row number

The following line is given:

Backticks[0].Folders[0] 

I need to increment the last number.

I'm not a Regexp replacement specialist, I tried this , but also selects the bracket.

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

Is it possible to make a choice, increment and replacement in one liner?

+4
source share
2 answers

can be done in one line

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

Use a positive result:

 /\d(?=]$)/gi 

This will make sure that after the character after the character after the character ends the character, not including it in the replacement.

If you want to increase it, you can use match and parseInt :

 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 it.

Here is the fiddle.

+1
source

Source: https://habr.com/ru/post/1479833/


All Articles