I’ve been looking for this for hours, right now I’ve ended in a very ugly way (but working). I would like to find a reusable nice way to do this.
I’ve a string like this:
wanna[0].some[0].javascript
I would like to replace the last digit occurence between square brackets:
wanna[0].some[1].javascript
I’ve ended this (ugly) way:
myString.replace(/\d].javascript$/, "1].javascript")
which should be the best regex to match that?
myString.replace(/\d/, 1) // this should be for the first digit
myString.replace(/\d/g, 1) // this for every digit
I’ve read about negative look-ahead but I still didn’t get if JS supports this.
Just use a negative lookahead to ascertain that there are no more brackets after the one you’re matching:
Here’s the fiddle: http://jsfiddle.net/bRkEP/