To my knowledge [ab]
and (a|b)
should be equivalent in purpose when trying to match against a set of characters. Now, look at two regex:
/^(\s|\u00A0)+|(\s|\u00A0)+$/g
/^[\s\u00A0]+|[\s\u00A0]+$/g
They should both match against whitespaces at the beginning and end of a string (See section on Polyfill here for more info on the regex itself). When using square brackets all things work well, but when you switch to parenthesis even the simplest of strings causes the browser to run seemingly indefinitely. This happens on latest Chrome and Firefox.
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,''));
Is this a crazy quirk with the browser's implementation of the regex engine or is there something else about the regex's algorithm which causes this?