Regex test causes 'Too much recursion' error in Javascript

1.3k Views Asked by At

I've been implementing validations for the input using Javascript and Regex. I found interesting (at least I think so) behavior with one particular Regex.

/([a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi

This Regex is for the URL input value validation.

Validation is triggered on user input event with some timeout like that:

<input onInput={(event) => this.inputChangedHandler(event)}} />

And the validation part:

...

if (!this.urlPattern.test(this.inputValue)) { // Here I am testing URL regex mentioned above
      this.isValid = false;
      this.validationErrorMessage = 'Please, check URL';
}

...

And in that step I am getting 'Too much recursion error'. In Chrome and Firefox. All other regexes I used in a same way to validate other input types (like email, number, etc etc) working perfectly well.

So my questions are:

Is that behavior expected for large regexes ? Is my regex wrong in some way ?

I have tried this regex to match the string value like this:

this.inputValue.match(this.urlPattern)

Which worked for me well too.

I found some bug report here: bugzilla

Large number of groups in regex causes too-much-recursion crash (YARR)

Seems like really similar to my issue.

0

There are 0 best solutions below