cdn34 cdn34 - 1 year ago 80
HTML Question

Finding a html tag by its content using regex (JS)

What I want to do is find the tag that has the string "test string" even when that tag is nested inside other tags.

HTML example:

<section class="test-class1"><div><p class="test-class2">something else....test string</p></div></section>

Regex :

/.*<([a-zA-Z]*).*>.*?test string/g



I'm using, for the testing;

This regex works well when the html is small, but when the size of the HTML increases, it times out.

Is there a way to improve the performance of the regex ?

Answer Source

< *(\w+)[^<>]*>[^<]*(?:<[^>]*)*test string

matches p in the first capturing group ($1). Is not possible to speed it up so much. You'd better to use pure JS functions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download