Note that there are some explanatory texts on larger screens.

plurals
  1. POGet substring from html string in javascript
    primarykey
    data
    text
    <p>I am officially declaring myself as dumb !!! I'm quite good with regex but the javascript regex is getting on my nerves:</p> <p>I have following html string:</p> <pre><code>htmlString = '&lt;div class="aa"&gt;TextOne&lt;/div&gt;&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;/ul&gt;'; </code></pre> <p>I need to get all that is inside the UL element based on the text that is inside the aa class div.</p> <p>I tried the following:</p> <pre><code>textItem = 'TextOne'; ulRegex = new RegExp('&lt;div class="aa"&gt;'+textItem+'&lt;/div&gt;&lt;ul&gt;(.*)&lt;/ul&gt;', "igm"); ul = ulRegex.exec(htmlString); </code></pre> <p>While writing this question i discovered an error (one tiny extra character) in my regex that didn't let it match but for all those looking for something specific - javascript / regular expression / html string / html substring - its working fine.</p> <p><strong>Edited</strong></p> <p>I'm thankful for all the additions to this - but there is one additional aspect i'm using regex - being that i am matching a text item which i am getting through a variable first for the regex pattern.</p> <p><strong>Solution</strong></p> <p>Having received a few hints and suggestions i came up with the following which may help someone else as well:</p> <pre><code>htmlString = '&lt;div class="aa"&gt;TextOne&lt;/div&gt;&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;/ul&gt;'; textItem = 'TextOne'; tempdiv = $('&lt;div/&gt;'); tempdiv.html(htmlString); ul = tempdiv.find('div.aa:contains('+textItem+')').next('ul'); $('#res').append(ul); </code></pre> <p><a href="http://jsfiddle.net/sdXpJ/" rel="nofollow">http://jsfiddle.net/sdXpJ/</a></p> <p>The next ul is important because that solves the issue regarding nested ULs and any other regex based solution where i couldn't match a first level UL (having internal one or more Uls).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload