XRegExp freezes while testing specific value
msidlo opened this issue · 1 comments
I encountered unusual behaviour using XRegExp. With a specific string, the execution freezes. Following string was an input of a user. I use XRegExp for validation in browser and the browser tab freezes. I was able to reproduce this error with a simple js script.
const XRegExp = require('xregexp')
let VALUE_TO_TEST = 'prírode Detský raj"'
/**
* Following string freezes the execution.
* 'prírode Detský raj"'
*
* The problem starts with quotation mark and preceding characters.
* When I use quotation mark on different place in the string
* or I delete preceding word 'prírode'
* E. g.
* 'prírode "Detský raj',
* 'Detský raj"'
* everything works fine.
*/
console.log('Begin')
const xreg = XRegExp(`^$|^((\\p{L}|\\p{N}|[,.'&-]|\\s)+(\\p{Zs})?(\\p{L}|\\p{N})*)+$`)
const isValid = xreg.test(VALUE_TO_TEST)
console.log(`Valid: ${isValid}`)
console.log('end')
This sounds like a case of catastrophic backtracking. It can happen with or without XRegExp. You will almost certainly need to modify the structure of your regex to avoid the problem, though you don’t necessarily need to change what it matches in the process. See https://regular-expressions.info/catastrophic.html.
I‘m guessing that removing the (\\p{L}|\\p{N})* near the end of the regex will fix the problem. As far as I can tell, that part is superfluous anyway and changes nothing about what is or isn’t matched.
To help with readability, you can also change (\\p{L}|\\p{N}|[,.'&-]|\\s) to [\\pL\\pN\\s,.'&-], and (\\p{Zs})? to \\p{Zs}? (unless you really do need those capturing groups for backreferences that are used elsewhere.
Also, I’m guessing you probably intended for your regex to match Unicode nonspacing marks (\\p{Mn}) like accents, alongside categories L and N.