janhartmann
janhartmann

Reputation: 15003

Replace last digit occurrence in square brackets

I have a variable like:

var text = 'researchOrganisationTrait.keywords[0].freeKeyword[1].texts[en_GB]';

Which I wish to maintain the index of the last occurrence (dynamic added content)

I have tried using the code like:

var text = 'researchOrganisationTrait.keywords[0].freeKeyword[1].texts[en_GB]';
text = text.replace(/\[\d*](?!.*\[)/, '[newIndex]');
alert(text);

But this does not replace freeKeyword[1] with freeKeyword[newIndex]

How to I match the last occurrence of square digit?

JSFiddle: http://jsfiddle.net/4eALF/

Upvotes: 0

Views: 283

Answers (1)

falsetru
falsetru

Reputation: 369064

Append \d:

text = text.replace(/\[\d+](?!.*\[\d)/, '[newIndex]')

Upvotes: 4

Related Questions