/(?:([^:;\(\[]*):)?(.*)/
Can you explain it in detail?
PHP中文网2017-05-19 10:47:02
Part one: (?:XXXXX:)?
Part two: (.*)
The first part, does not match one or zero XXXXX:, XXXXX matches zero or more of these:;([Any characters other than the symbol, which is captured by the first capturing group.
The second part is to capture zero or more arbitrary characters.
曾经蜡笔没有小新2017-05-19 10:47:02
As explained on the first floor, there are two capture groups. The content of the brackets in the first capture group is after ([^:;([]*)
指匹配不包含:;([
字符的任意个字符,加上前面的 ?:
表示只匹配括号里面的内容但是不捕获,最后是匹配一个:
,(?:([^:;([]*):)?
, 匹配一个不包含:;([
这四个符号的字符串再加一个:
零次或一次;
第二个捕获组就是捕获任意字符串,就是说如果第一个捕获组匹配失败,那么第二个捕获组会获取整个字符串,
如果第一个捕获组成功,结果是捕获两个字符串,一个是:
之前的,一个是:
;
Example:
var re = /(?:([^:;\(\[]*):)?(.*)/;
re.exec('abc:123');
==> ["abc:123", "abc", "123", index: 0, input: "abc:123"]
re.exec('(abc:123');
==> ["(abc:123", undefined, "(abc:123", index: 0, input: "(abc:123"]