markdown-to-jsx icon indicating copy to clipboard operation
markdown-to-jsx copied to clipboard

Slow Regex HTML_BLOCK_ELEMENT_R because of issue with self closing tags

Open devbrains-com opened this issue 5 months ago • 0 comments

We found out, the following regex is very slow and takes up to 50ms with a single self closing tag on the page.

const HTML_BLOCK_ELEMENT_R = /^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?([^>]*)\/{0}>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1)[\s\S])*?)<\/\1>\n*/i

The reason for that seems to be a non working check for self closing tags \/{0}.

The final regex would be:

const HTML_BLOCK_ELEMENT_R = /^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?((?:[^>]*[^/])?)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1)[\s\S])*?)<\/\1>\n*/i

Thank you very much

devbrains-com avatar Feb 26 '24 13:02 devbrains-com