How can I find the last occurrence of a word with word boundaries? I created a regex expression of /\btotal\b/
for the word. How would I use search() to find the last occurrence of this expression? Thanks in advance for the help!
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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.You can use negative lookahead to get the last match:
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.