Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this code stuck node.js - Bug on Javascript?

I'm trying to run this regex but it stuck my console. Why?

var str = "Шедевры православной музыки - 20 золотых православных песен";
str.match(/^(([\u00C0-\u1FFF\u2C00-\uD7FF]+[^a-z\u00C0-\u1FFF\u2C00-\uD7FF]*)+) [a-z]+[^\u00C0-\u1FFF\u2C00-\uD7FF]*$/i);
like image 238
Moshe Simantov Avatar asked Feb 17 '16 10:02

Moshe Simantov


People also ask

Why is node js not working?

Problems occurring in Node. js application deployments can have a range of symptoms, but can generally be categorized into the following: Uncaught exception or error event in JavaScript code. Excessive memory usage, which may result in an out-of-memory error.

Are there bugs in JavaScript?

If your JavaScript doesn't work as you expected it to, that might mean you have a bug in your code. Typos are a common source of bugs in JavaScript. If your script isn't working, check the following: Missing punctuation.

What are the challenges with node js?

Scalability challenges js is a single-threaded process. This makes app scaling a bit difficult for Node. js web app developers. Moreover, a single-threaded process makes it practically impossible to execute other code in parallel.

What does node error mean?

An error in Node. js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError , RangeError , TypeError , URIError , EvalError , and SyntaxError .


1 Answers

Your regex causes catastrophic backtracking (see a demo of your regex here) due to (([\u00C0-\u1FFF\u2C00-\uD7FF]+[^a-z\u00C0-\u1FFF\u2C00-\uD7FF]*)+) part. As [^a-z\u00C0-\u1FFF\u2C00-\uD7FF]* can match zero characters, you basically have a classical (a+)+-like pattern (cf: ([\u00C0-\u1FFF\u2C00-\uD7FF]+)+) that causes backtracking issue.

To get rid of it, you need to make sure the subpatterns are compulsory inside the grouping, and apply a * quantifier to the whole grouping:

^([\u00C0-\u1FFF\u2C00-\uD7FF]+(?:[^a-z\u00C0-\u1FFF\u2C00-\uD7FF]+[\u00C0-\u1‌​FFF\u2C00-\uD7FF]+)*) [a-z]+[^\u00C0-\u1FFF\u2C00-\uD7FF]*$

See regex demo

Here, [\u00C0-\u1FFF\u2C00-\uD7FF]+(?:[^a-z\u00C0-\u1FFF\u2C00-\uD7FF]+[\u00C0-\u1‌​FFF\u2C00-\uD7FF]+)* matches:

  • [\u00C0-\u1FFF\u2C00-\uD7FF]+ - one or more character from [\u00C0-\u1FFF\u2C00-\uD7FF] ranges
  • (?:[^a-z\u00C0-\u1FFF\u2C00-\uD7FF]+[\u00C0-\u1‌​FFF\u2C00-\uD7FF]+)* - zero or more sequences of:
    • [^a-z\u00C0-\u1FFF\u2C00-\uD7FF]+ - one or more characters other than those from the a-z\u00C0-\u1FFF\u2C00-\uD7FF ranges
    • [\u00C0-\u1‌​FFF\u2C00-\uD7FF]+ - one or more characters from the \u00C0-\u1‌​FFF\u2C00-\uD7FF ranges.
like image 98
Wiktor Stribiżew Avatar answered Sep 17 '22 23:09

Wiktor Stribiżew