0

I want to find last matched word in a sentence using regex. I searched for this functionality and found negative lookahead.

Ex: To find "foo" in "This foo is not same as the foo"

Here i need to find last matched "foo" 's index. For this I am using javascript search function with search(regex) where regex is foo(?!.*foo)

problem is that search is not returning correct index (it may be regex issue).This feature is same as chrome's find feature where up arrow finds previous match.So I want to know if i need to change my regex ?

Thanks

Bhuwan
  • 225
  • 5
  • 15

1 Answers1

1

To find last matched "foo" 's index. You can use String.prototype.lastIndexOf()

var lastIndex = 'This foo is not same as the foo'.lastIndexOf('foo');

console.log(lastIndex);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46