在JavaScript中,空的正则表达式模式是否定义了行为?

前端之家收集整理的这篇文章主要介绍了在JavaScript中,空的正则表达式模式是否定义了行为?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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","=","+","-","]","
"]

猜你在找的JavaScript相关文章