Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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,我在Java环境中使用了这个正则表达式,但无法使其在Javascript上正常工作: /^(?:select\s+)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*\,\s*)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*from\s+)([\w\d\.]+)\s*([\w\d]+)?$/g 此表达式应与以下类型的输入行匹配: select a.id, a.name from public.blah a select a, a.name from p

我在Java环境中使用了这个正则表达式,但无法使其在Javascript上正常工作:

/^(?:select\s+)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*\,\s*)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*from\s+)([\w\d\.]+)\s*([\w\d]+)?$/g
此表达式应与以下类型的输入行匹配:

select a.id, a.name from public.blah a

select a, a.name from public.blah a

select id, name from blah

a.id, name from brah a

from blah a
对于我期望的群体:

1:第一个投影别名(a)(如有)

2:第一个投影属性(id)(如果有)

3:第二个投影别名(a)(如有)

4:第二个投影属性(名称)(如有)

5:关系名称(public.blah)

6:关系别名(a)(如有)

正则表达式在Javascript上匹配,但它无法获取组。一定有什么我需要改变的,使它的工作

var optionsPattern = /^(?:select\s+)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*\,\s*)?(?:([\w\d]+)?\.?([\w\d]+))?(?:\s*from\s+)([\w\d\.]+)\s*([\w\d]+)?$/g;

var text = 'select id, text from options.test';

var match = text.match(optionsPattern);

if(match) {
  alert(match[0]); // select id, text from options.test
  alert(match[1]); // undefined 
}
你们有什么需要改变的线索吗


谢谢大家!

删除
g
标志。它正在将您的正则表达式从“匹配并返回子模式”更改为“搜索并返回所有匹配”

Works!我瞎了!谢谢。无论如何,我必须做一个小小的更改,因为属性的第一个字母被视为别名,所以最后它是:
/(?:(?:([\w\d]+)\)([\w\d]+))([\w\d]+)((?:\s*,\s*)(?:([\w\d]+)\)([\w\d]+)(?:\s*from\s+)([\w\d]+)(?:\d]+)(?:\s*)$/