var pattern = /(?:)/
从我的测试,似乎匹配一切.这是定义的行为吗?
解决方法
这并不直接回答这个问题,但是这里有关于空的正则表达式的规范呢?
从15.5.4.14 String.prototype.split (separator,limit)
The value of separator may be an empty String,an empty regular expression,or a regular expression that can match an empty String.
从7.8.5 Regular Expression Literals
NOTE Regular expression literals may not be empty; instead of representing an empty regular expression literal,the characters // start a single-line comment. To specify an empty regular expression,use:
/(?:)/
.
所以假定它是.split()中的分隔符的可接受的值,我猜想它是定义的行为,作为分割每个字符的一种方式.
"fjeij;als#%^&é.\n isoij\t;oi`1=+-]\r".split(/(?:)/); ["f","j","e","i",";","a","l","s","#","%","^","&","é","."," "," ","o"," ","`","1","=","+","-","]"," "]