开发者

/regex/(string) considered harmful?

In开发者_JAVA百科 javascript you can match regular expression with regex(string) syntax.

However the syntax isn't very used as far as I know, anybody know why?


On some browsers calling a regular expression as if it where a function, regexp(str) is equivalent to call the exec method.

This is not recommended because the syntax is not part of the ECMA-262, Edition 3 Specification, and is not guaranteed to work:

var result1 = regexp.exec(str); // part of the ECMA-262
var result2 = regexp(str);


Because it's much harder to write. If you use a string, you have to escape your escape characters, etc. In general, it's a royal pain and about 10 times harder to read later on.

The only reason I've ever used it is when I had to store a regex and retrieve it later from a database. In that case, you're kinda stuck.

Edit: misunderstood the question - not creation of the regex, execution of it. Whoops.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜