Regex / pipe string extraction help


#1

Need to return a substring between two specified strings.

I have this excerpt from an XML url.

<div class="hs-featured-image-wrapper"> <a href="http://news.hurstfs.com/blog/fact-vs.-myth-best-ways-to-manage-your-garden-and-flower-beds" title="" class="hs-featured-image-link"> <img src="http://news.hurstfs.com/hubfs/image1%20copy%202.png?t=1496778496754" alt="image1 copy 2.png" class="hs-featured-image" style="width:auto !important; max-width:50%; float:left; margin:0 15px 15px 0;"> </a> </div> <p><span style="font-weight: 400;">Getting ready to garden? Here are some tips to ensure that this year’s garden is a success.</span></p>

I need to return the img src and the actually description that is merged with all this formatting

here is what I have so far…

<h6>
        {{entry.getElementsByTagName('description')[0].textContent | regexp() }}<br><br>
</h6>

I was using SLICE, but i need to get the string between two specific strings instead of the indexes.


#2

I think you would be better off using an XML parser library. Regular expressions are not capable of doing this robustly.


#3

thanks. i will try to look into that. do you have any specific examples?


#4

Figured it out!! I ended up implementing a custom pipe to filter out the unneeded text/tags.

transform(value: string, …args) {
return value.split(‘Starting string to remove from behind’).pop().split(‘ending string to remove from after’).shift();
}

works for something like this…

unneededstringNEEDEDSTRINGuneededstring