.split () regex with sliding brackets doesn't work in IE8

I have a regex that breaks a string into an array in each space and stores the value of the space in each case as follows:

var str = "[This is a test]";
var foo = str.toLowerCase().split(/(\S+\s+)/).filter(function(n) {return n});

This returns the following in all modern browsers:

["[This ", "is ", "a ", "test]"];

But on IE8, all I get is ["test]"];

IE8 doesn't seem to read the regex character \Scorrectly. Does anyone know of a workaround for IE8 to play the correct array?

thank

+4
source share
1 answer

I don’t understand why you complicate things using split()callbacks as well. Just combine, not split!

/\S+\s*/g

  • \S+: .
  • \s*: .
  • g    : , .

. IE8 Array.filter().

-

+4

Source: https://habr.com/ru/post/1529179/


All Articles