question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Special RegExe `\b` Causes Marking to Freeze

See original GitHub issue

When 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:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Mottiecommented, Sep 12, 2016

LOL, I just noticed that… let me see if I can come up with a better solution then.

1reaction
Almenoncommented, Sep 12, 2016

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found