# JS-Interpreter RegExp Demo

Pathological regular expressions can execute in geometric time. For example `'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac'.match(/^(a+)+b/)` will effectively crash any JavaScript runtime. JS-Interpreter has three modes for handling regular expressions.

## REGEXP_MODE = 0

In mode 0, all regular expressions are disabled and throw an error. This is safe and works in every environment.

// All regular expressions will fail here. try { alert('hello'.search(/(.)\1+/)); } catch (e) { alert(e); }

## REGEXP_MODE = 1

In mode 1, all regular expressions are executed natively. This is potentially unsafe and works in every environment.

// This regular expression is fine and returns 'll'. alert('hello'.match(/(.)\1+/)[0]);

## REGEXP_MODE = 2 (default mode)

In mode 2, all regular expressions are executed in a separate environment. In web browsers this means asynchronously in a Web Worker thread, in Node.js this means synchronously in a VM. If a regular expression takes more than REGEXP_THREAD_TIMEOUT (default 1000 ms) then it is terminated and an error thrown. This is safe and works in most environments. If Web Workers or VM is not supported (meaning IE 9 or IE 10), then all regular expressions throw an error.

// This regular expression is fine and returns 'aaac'. alert('aaac'.split(/^(a+)+b/)); try { // This regular expression is pathological and hits the timeout. alert('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac'.split(/^(a+)+b/)); } catch (e) { alert(e); }

Back to the JS-Interpreter documentation.