I wish to replace the a subquery to the table name inside the subquery using regex in node.js.
e.g. converting the following query:
SELECT col1
FROM (SELECT col1::DECIMAL(10),
col2,
FROM @table_name)
WHERE (COND1) AND (COND2)
To:
SELECT col1
FROM @table_name
WHERE (COND1) AND (COND2)
I have tried using:
string.replace(/\([^()]*\)/g, '')
But it eliminates the entire content.
Any idea is appreciated. Thanks!
You could try the following regex replacement:
var sql = `SELECT col1
FROM (SELECT col1,
col2,
FROM @table_name)
WHERE (COND1) AND (COND2)`;
sql = sql.replace(/\([^)]*\bFROM (@[^)]+)[^)]*\)/, "$1");
console.log(sql);
But to cover all possible edge cases, you might want to use a SQL parser.