:::: MENU ::::

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

jquery_logo

 

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.

i.e.

jQuery

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

i.e.

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

 

Share and Enjoy

  • DZone
  • Google Plus
  • LinkedIn
  • Twitter
  • Facebook
  • Email

So, what do you think ?

  •