简体   繁体   中英

Timeout errors with javascript regex

I have a content with <del></del> and <ins></ins> tags. I have a regex which satisfies the conditions which are clearly explained in the image

在此处输入图片说明

The regex which satisfies the conditions is:

This regex is working perfectly fine. But, when the content is more or When the matching content is long the execution time is increasing and hence throwing a timeout error which can be seen in

How to simplify the regex

(\w*(?:(?:(?:<del\b[^>]*>(?:(?!<\/del>).)*<\/del>)|(?:<ins\b[^>]*>\w+<\/ins>)|(?:\w+<\/ins>)|(?:<ins\b[^>]*>\w+))(?:\w+|))+) 

to avoid it from throwing timeout error?

Regex is not correct way to do this.You yourself have discovered a pitfall. There will be more.So switch to DOM parser .As for removing the timeout issue , you can try

((?=(\w*))\2(?:(?:(?:<del\b[^>]*>(?:(?!<\/del>).)*<\/del>)|(?:<ins\b[^>]*>\w+<\/ins>)|(?:\w+<\/ins>)|(?:<ins\b[^>]*>\w+))(?:\w+|))+)

See demo.

https://regex101.com/r/cE4mE3/24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM