Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Cowling 144 posts 343 karma points
    Jan 29, 2018 @ 17:04
    Tom Cowling
    0

    Combining html.raw and regex

    Hi everyone,

    I'm using a search plugin (SimplisticSearch) which for the most part is brilliant. I'm having an issue with stray HTML finding it's way into my code though.

    I have the title as the link to the node and then a short description underneath which is pulled from the main body text of the page. In this text, I'm getting image tags and a hrefs and I would like to remove them.

    I've had a look around and found (what I think) is the correct RegEx code to do this, but I'm struggling to get it working. Code below:

    <ul class="search-result">
    @foreach (var node in searchResults)
    {
        <li style="padding: 0 20px; border-bottom: 1px dashed #ccc;">
            <h4><a href="@node.Url">@node.Name</a></h4>
            <p>@Html.Raw(node.Description ?? "")</p>
        </li>
    }
    

    The (correct?) RegEx to use:

    Regex.Replace(node.Description, "<.*?>", string.Empty)
    

    If anyone has an elegant solution that just uses razor instead, that's also fine!

    Thanks,

    Tom

  • Nigel Wilson 945 posts 2077 karma points
    Jan 29, 2018 @ 18:26
    Nigel Wilson
    0

    Hi Tom

    Not tested but there is/was the following helper...

    @Umbraco.StripHtml(YOURSTRING)
    

    Cheers Nigel

Please Sign in or register to post replies

Write your reply to:

Draft