Find Last Occurrence of Regex Word
You can use negative lookahead to get the last match:
/(\btotal\b)(?!.*\b\1\b)/
RegEx Demo 1
RegEx Demo 2
(?!.*\1)
is negative lookahead to assert that captured group #1 i.e. total
word is NOT present ahead of the present match.
Without using the lookaheads but using the same regex (having applied the g
, i.e. global, flag), the option would be to match the string with regex and get the last match.
var matches = yourString.match(/\btotal\b/g);
var lastMatch = matches[matches.length-1];