Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

javascript - Find Last Occurrence of Regex Word

How can I find the last occurrence of a word with word boundaries? I created a regex expression of /total/ for the word. How would I use search() to find the last occurrence of this expression? Thanks in advance for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use negative lookahead to get the last match:

/(total)(?!.*1)/

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...