Special RegExe `\b` Causes Marking to Freeze
See original GitHub issueWhen highlighting with regular expression some of them cause browsers to hang and the script stops responding. For example providing only \b
as a regex.
I am working on an app where user can provide custom regex that is used for highlighting and we highlight on every change of the input. Imagine I want to highlight \bJohn\b
which is a valid regex. When the user types \b
everything freezes. How can I detect this type of a regexes and skip highlighting for them?
Steps to reproduce
Open Custom regular expression highlighting fiddle provided in the documentation, input this regular expression /\b/gmi
and click Mark
. Instead of highlighting nothing my browsers hangs.
Environment
- Safari, Crome
- mark.js version 8.1.1
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Regexp causes to hang infinitely - Stack Overflow
The nested quantifiers cause too much backtracking that you can see on the regex debugger page at regex101.com. A solution is to either...
Read more >Improving the performance of regular expressions
Firstly, the \b.* was removed from the start of the regular expression, since it served no purpose other than to slow the whole...
Read more >How to protect against regex denial-of-service (ReDoS) attacks
Learn some tips to help you safeguard regular expressions against denial-of-service (DoS) attacks, known as ReDoS attacks.
Read more >Best Practices for Regular Expressions in .NET - Microsoft Learn
Matches zero or more occurrences of one or more word characters that are followed either by zero or one carriage return and a...
Read more >Matching a particular regular expression causes hang/freeze!
Issue 2655: Matching a particular regular expression causes hang/freeze! ... Node will hang. Chrome will hang if this JS is in a page....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
LOL, I just noticed that… let me see if I can come up with a better solution then.
What a coincidence - I’m also working on a website where users can provide custom regex to be highlighted, and I have the same problem. If you pause the Javascript you can see that the code is stuck on a loop where it continuously generates new empty mark elements.
The loop happens on this line: https://github.com/julmot/mark.js/blob/master/dist/mark.js#L453