:::: MENU ::::

jQuery :contains() selector regex for matching text within elements



jQuery contains() selector simply matches text within selectors.  It’s also case sensitive, so we’ve very limitation for selecting or finding elements match approximately.



How about links those are inserted dynamically into DOM and we want to make sure if they exist before manipulation of some action?


What If we want to find, links containing text “Example” or “Assignment” exist?
If yes, how many?

Here jQuery contains() selector won’t work because it needs explicit arguments like $(‘a:contains(“Example 1”)’)

So, here regular expression can do the job.

Regular expression for matching link text.

 Matching with Text nodes

 Using regular expression with jQuery anchor selector

Final Code

Find number of links that matches regular expression


So, what do you think ?