Regex causing browser crash

Due to the following regex, the browser crashes or waits for an indefinite time

The problem looks like due to catastrophic backtracking.
Is there a way to improve this regex so that browser doesn’t crash while doing a backspace?

/^(?=.{1,254}$)[a-zA-Z0-9](?:(?:[-.]?[a-zA-Z0-9]){0,62})+$/

79 thoughts on “Regex causing browser crash”