SQL SELECT Statement in JS
Match the column list and table name from a SQL SELECT ... FROM statement.
Try it in the JS tester →Pattern
regexJS
SELECT\s+(.+?)\s+FROM\s+([\w."`\[\]]+) (flags: gis)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("SELECT\\s+(.+?)\\s+FROM\\s+([\\w.\"`\\[\\]]+)", "gis");
const input = "SELECT id, name FROM users";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
SELECT\s+ matches the keyword and required whitespace. (.+?) lazily captures the column list. \s+FROM\s+ matches the FROM keyword. ([\w."`\[\]]+) captures the table identifier including dots (db.schema.table), and the three quoting styles SQL dialects use: "double", `backtick`, [bracket]. Flags: g (global), i (case-insensitive SELECT/FROM), s (dotAll so columns can span newlines).
Examples
Input
SELECT id, name FROM usersMatches
SELECT id, name FROM users
Input
select * from `orders`Matches
select * from `orders`
Input
INSERT INTO logsNo match
—