Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可选结束捕获组_Javascript_Regex - Fatal编程技术网

Javascript 可选结束捕获组

Javascript 可选结束捕获组,javascript,regex,Javascript,Regex,我尝试总共有三个捕获组,其中最后一个是可选的。我遇到了一个问题,我不确定如何从最后一个组中排除最后一个分隔符,并让它查找到底 这是我的模式 /(.+)\@(.+)\:(.+)/ 下面是我的示例字符串 test@hello // => ['test', 'hello'] test@hello:optional // => ['test', 'hello', 'optional'] 使用string.split string.split(/[:@]/) [:@]匹配冒号或@符号,然

我尝试总共有三个捕获组,其中最后一个是可选的。我遇到了一个问题,我不确定如何从最后一个组中排除最后一个分隔符,并让它查找到底

这是我的模式

/(.+)\@(.+)\:(.+)/
下面是我的示例字符串

test@hello // => ['test', 'hello']
test@hello:optional // => ['test', 'hello', 'optional']
使用string.split

string.split(/[:@]/)
[:@]
匹配冒号或
@
符号,然后split函数根据匹配的字符拆分字符串

var='s'test@hello:optiona'
警报(s.split(/[@::]/)
使用string.split

string.split(/[:@]/)
[:@]
匹配冒号或
@
符号,然后split函数根据匹配的字符拆分字符串

var='s'test@hello:optiona'

警报(s.split(/[@::]/)
仅显示如何修复模式:。仅显示如何修复模式:。