skip to Main Content
<(?<nodeName>[a-zA-Z][-a-zA-Z0-9_]*)(?<attributes>(?:s*[a-zA-Z_:!0-9,][-a-zA-Z0-9_:%."'`;(]*(?:s*=s*(?:(?:"[^"]*")|(?:'[^']*')|{{[^>}]*}}|[^>]+|w*))?)*)s*(/?)>

This above regex trying to match the tagName and attributes.

<span class='Utils.showtt("{{getI18n('zr.ratings.reviews.client.review.tag', getCrmModuleInfo('Accounts', 'true'))}}")'>

but getting infinite loop since quotes mismatched in this input. Is there any way to throw error or prevent infinite loop.

2

Answers


  1. <(?<nodeName>[a-zA-Z][-a-zA-Z0-9_]*)(?<attributes>(?:s*[a-zA-Z_:!0-9,][-a-zA-Z0-9_:%."'`;(]*(?:s*=s*(?:(?:"[^"']*")|(?:'[^'"]*')|{{[^>}]*}}|[^>]+|w*))?)*)s*(/?)>
    

    try this regex it will match entire attribute irrespective of quotes.

    Login or Signup to reply.
  2. Regex is the wrong tool for parsing HTML. Instead use a DOM Parser — like DOMParser.

    Note that your input string is not valid HTML: the attribute value is closed by the second quote. That quote should be escaped in the context of an attribute value. As you have several single quotes, it might be better to enclose the attribute in double quotes and escape the double quotes. So for instance, the input could be valid like this:

    <span class="Utils.showtt(&quot;{{getI18n(zr.ratings.reviews.client.review.tag', getCrmModuleInfo('Accounts', 'true'))}}&quot;)">
    

    Example of using DOMParser:

    const html = `<span class="Utils.showtt(&quot;{{getI18n(zr.ratings.reviews.client.review.tag', getCrmModuleInfo('Accounts', 'true'))}}&quot;)">`;
    
    const elem = new DOMParser().parseFromString(html, "text/html")
                                .body.children[0];
    console.log(elem.tagName); // SPAN
    for (const attr of elem.attributes) {
        console.log(attr.name, "=", attr.value);
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search