最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - JavaScript RegExp Optional Capture Group - Stack Overflow

programmeradmin2浏览0评论

Given these two strings:

let query = "select color, year from car where color = blue and year = 2002";

or

let query = "select color, year from car";

I created the following RegExp with String.prototype.match to extract the columns, the table name and the where clause (optional)

var results = query.match(/select (.*) from (.*) where (.*)/);

For the first query, the result is what I was expecting:

[ 
  'select color, year from car where color = blue and year = 2002',
  'color, year',
  'car',
  'color = blue and year = 2002',
  index: 0,
  input: 'select color, year from car where color = blue and year = 2002' 
]

For the second query, I got a null, and if I try to change the RegExp to consider the where clause optional, it changes the capture groups and also does not work:

var results = query.match(/select (.*) from (.*)( where (.*))?/);

Any idea about how to use capture groups, where one of those are optional, in this situation? I have other strategies to implement that but I really would like to keep using the capture groups.

Thanks

Given these two strings:

let query = "select color, year from car where color = blue and year = 2002";

or

let query = "select color, year from car";

I created the following RegExp with String.prototype.match to extract the columns, the table name and the where clause (optional)

var results = query.match(/select (.*) from (.*) where (.*)/);

For the first query, the result is what I was expecting:

[ 
  'select color, year from car where color = blue and year = 2002',
  'color, year',
  'car',
  'color = blue and year = 2002',
  index: 0,
  input: 'select color, year from car where color = blue and year = 2002' 
]

For the second query, I got a null, and if I try to change the RegExp to consider the where clause optional, it changes the capture groups and also does not work:

var results = query.match(/select (.*) from (.*)( where (.*))?/);

Any idea about how to use capture groups, where one of those are optional, in this situation? I have other strategies to implement that but I really would like to keep using the capture groups.

Thanks

Share Improve this question edited Mar 23, 2017 at 13:00 krampstudio 3,6312 gold badges44 silver badges65 bronze badges asked Mar 23, 2017 at 12:42 John JacksonJohn Jackson 731 silver badge3 bronze badges 1
  • just use "non remembering" grouping parens: query.match(/select (.*) from (.*)(?:where (.*))?/) – Scott Weaver Commented Mar 23, 2017 at 12:50
Add a ment  | 

1 Answer 1

Reset to default 5

The answer is to surround the optional WHERE clause with a non-capturing group and make that optional. I've also change it slightly because you probably don't want to match any character .. You can always change that back.

var regex = /select\s+([\w ,]+)\s+from\s+(\w+)(?:\s+where([\w =]+))?/gi;

DEMO

I would be remiss though if I didn't mention that processing code with regexes is a terrible idea. You can always use an SQL parser.

发布评论

评论列表(0)

  1. 暂无评论