0

I'm trying to get the tables and aliases of a SQL select statement, for example the following select:

select * from table1 t1, table2, table3 t3, table4 where column = 1

should return two capturing groups, the first one table1, table2, table3 and table4. The second capturing group t1, null, t3 and null.

My attempt returns only the first item of each capturing group:

from\s+([^ ,]+)\s*(\w+)*,

returns table1 and t1 only. How to expand the regex to retrieve all the occurrences?

ps0604
  • 1,227
  • 23
  • 133
  • 330

1 Answers1

1

Assuming str is your string

var tables = str.match(/from\s(.*)\swhere/)[1].split(/, /);

Returns:

["table1 t1", "table2", "table3 t3", "table4"]
Downgoat
  • 13,771
  • 5
  • 46
  • 69