在JavaScript中,空的正则表达式模式是否定义了行为? [英] In JavaScript, does an empty regex pattern have defined behavior?
问题描述
var pattern = /(?:)/
从我的测试来看,它似乎与一切相符。这是定义的行为吗?
From my testing, it seems to match everything. Is this the defined behavior?
推荐答案
这不能直接回答这个问题,但这里有关于该规范的说法空正则表达式:
This doesn't directly answer the question, but here's what the spec has to say about the empty regular expression:
来自 15.5.4.14 String.prototype.split(分隔符,限制)
separator可以是空字符串,空正则表达式,或者可以匹配空字符串的正则表达式。
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正则表达式文字
注意正则表达式文字可能不为空;字符//开始单行注释,而不是表示空的正则表达式文字。要指定空的正则表达式,请使用:
/(?:) /
。
因为它是 .split()
中分隔符的可接受值,我猜它是定义的行为,作为分割每个字符的一种方式。
So given that it is an accepted value for the separator in .split()
, I would guess that it is the defined behavior as a way to split on every character.
"fjeij;als#%^&é.\n isoij\t;oi`1=+-]\r".split(/(?:)/);
["f", "j", "e", "i", "j", ";", "a", "l", "s", "#", "%", "^", "&", "é", ".", "
", " ", "i", "s", "o", "i", "j", " ", ";", "o", "i", "`", "1", "=", "+", "-", "]", "
"]
这篇关于在JavaScript中,空的正则表达式模式是否定义了行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!