Regular Expression Worker



Execute Regular Expression Matches on a Node Worker Thread or in a Web Worker.

Regular Expressions can suffer from Catastrophic Backtracking . A very simple expression like /(x+x+)+y/ can cause your JavaScript application to freeze. This library allows you to run these expressions in another thread, keeping your application responsive. If they take to long to complete, they are terminated, protecting your application from locking up.

The playground below allows you to test the worker with different regular expressions and content. It will show you the elapsed time for the the worker to process the regular expression against the content.

Note: The timings shown are rounded to the nearest 1/10 of a millisecond. This is due to the crossOriginIsolated property. See also: Performance: now() method - Web APIs | MDN.


RegExp Worker Playground

RegExp:
(^`{3,}).*$[\s\S]*?\1$ # Match against code blocks | (hello(?<postfix>[a-z_-])) | # Match against the word "hello" (?<=(^|\s)`).*?(?=`(\s|$)) # Match against back tick quotes
(^`{3,}).*$[\s\S]*?\1$ # Match against code blocks | (hello(?[a-z_-])) | # Match against the word "hello" (?<=(^|\s)`).*?(?=`(\s|$)) # Match against back tick quotes
Flags:
The `regexp-worker` worker will run regular expressions in a web worker. This is useful to help avoid blocking the main thread when running complex or long-running regular expressions. It can be used to protect your application from untrusted regular expressions, as they will run in a separate thread. If the regular expression takes too long to run, it will be terminated with a TimeoutError. The example regular expression `(?<=(^|\s)`).*?(?=`(\s|$))` matches everything between `back tick quotes` on the same line. Example Regular Expression with Catastrophic Backtracking: `(x+x+)+y` <-- use this expression. Sample text: `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy` <-- remove the `y` to cause it to time out. ```javascript import { createRegExpWorker } from 'regexp-worker'; const worker = createRegExpWorker(); ```
The `regexp-worker` worker will run regular expressions in a web worker. This is useful to help avoid blocking the main thread when running complex or long-running regular expressions. It can be used to protect your application from untrusted regular expressions, as they will run in a separate thread. If the regular expression takes too long to run, it will be terminated with a TimeoutError. The example regular expression `(?<=(^|\s)`).*?(?=`(\s|$))` matches everything between `back tick quotes` on the same line. Example Regular Expression with Catastrophic Backtracking: `(x+x+)+y` <-- use this expression. Sample text: `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy` <-- remove the `y` to cause it to time out. ```javascript import { createRegExpWorker } from 'regexp-worker'; const worker = createRegExpWorker(); ```