Regular Expression just takes forever on a non recognized character

79 Views Asked by At

I have a crazy regular expression that I m trying to run which accepts all language characters and kanji characters as well. But is takes forever and 99% of the time just hangs my UI. I am really new to regular expression. Any help to fix this regular expression would be really helpful. Thanks.

RegExp = XRegExp("^(\\p{L}|[0-9\\n\\[\\]\\(\\)\\#\\*\\“\\-\\+\\\\\\/\\.\\s\\:\\_\\“\\”\\–\\?\\!\\,\\;\\'\\’\\‘\\"\\&\\¡\\¿\\«\\»\\„\\。\\、\\「\\」\\『\\』]|[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195])+$")
1

There are 1 best solutions below

0
On

You should not use ^(a|b|c)+$ like patterns since the backtracking here will be much more extensive than in the case of a mere character class.

You can merge, concatenate all your single char patterns into a single character class:

var pattern = "^[-–\\p{L}0-9\n[\\]()#*+\\\\/.\\s:_“”?!,;'’‘"&¡¿«»„。、「」『』\\u3000-\\u303F\\u3040-\\u309F\\u30A0-\\u30FF\\uFF00-\\uFFEF\\u4E00-\\u9FAF\\u2605-\\u2606\\u2190-\\u2195]+$";

It will work much faster.

Also, if you are using it to allow " and & as char sequences, you need to move them out of the character class:

var pattern = "^(?:&(?:quot|amp);|[-–\\p{L}0-9\n[\\]()#*+\\\\/.\\s:_“”?!,;'’‘¡¿«»„。、「」『』\\u3000-\\u303F\\u3040-\\u309F\\u30A0-\\u30FF\\uFF00-\\uFFEF\\u4E00-\\u9FAF\\u2605-\\u2606\\u2190-\\u2195])+$";