chai
chai

Reputation: 21

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

enter image description here

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?

Upvotes: 1

Views: 328

Answers (1)

vks
vks

Reputation: 67968

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

Upvotes: 1

Related Questions