1

I'm trying to select all non whitespace characters. But regular expression \S* or [^\s]* failing with "JavaScript heap out of memory" error for any string.

I tried it with node and directly on browser console. However when I tried it with an online regex tester then it worked fine.

var validAttrStrRegxp = new RegExp("\\S*", "g");
getAllMatches("any string",validAttrStrRegxp);

var getAllMatches = function(string, regex) {
  var matches = [];
  var match = regex.exec(string);
  while (match) {
    var allmatches = [];
    for (var index = 0; index < match.length; index++) {
        allmatches.push(match[index]);
    }
    matches.push(allmatches);
    match = regex.exec(string);
  }
  return matches;
};
Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90

1 Answers1

2

I got the reason. As * means 0 or many. The RE I specified also means to match for empty pattern. Hence the issue is not being caused with \\S+.

Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90