Why is this JavaScript request being called in a browser?

As far as I know, [ab]they (a|b)should be equivalent when trying to match a character set. Now consider two regular expressions:

/^(\s|\u00A0)+|(\s|\u00A0)+$/g
/^[\s\u00A0]+|[\s\u00A0]+$/g

They must match spaces at the beginning and end of the line (see the Polyfill section here for more information on regular expression itself). When using square brackets, everything works fine, but when you switch to brackets, even the simplest line makes the browser start seemingly endlessly. This happens in the latest Chrome and Firefox browsers.

This jsfiddle demonstrates this:

a ="a                                                               b";

// Doesn't work
// alert(a.replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,''));
// Works
alert(a.replace(/^[\s\u00A0]+|[\s\u00A0]+$/g,''));

- ?

+4
1

, , , .

, :

a = Array(30).join("\u00a0") + "b";  // A string with 30 consecutive \u00a0
s = Date.now();
t = a.replace(/^(\s|\u00A0)+$/g, '');
console.log(Date.now()-s, a.length);

: ^(\s|\u00A0)+$. , \s , \u00A0. , \s \u00A0 30 \u00A0 .

, /(\s|\u00A0)+/, , 2^30 30 . 30 , ($) , 2^30.

( jsfiddle, "" ) a \u00a0 \u00a0 ... \u00a0 b 30 \u00A0, 2^30. , .

+6

All Articles