I have a query :
select a.id, a.nama, b.alamat from tsql as a join tswl as b on a.id = b.id join tscl as c on a.id=c.id
And using regex I wanted to get the table name from the query string. 得到表名。
I tried using the regular expression :
select.*from\s+(\w+).*join\s(\w+).*
Which managed to get the first table name after "from", but skipped the second table name, and get the third table name.
Current group : [Group 1 : "tsql", Group 2 : "tscl"]
Targeted group : [Group 1 : "tsql", Group 2 : "tswl", Group 3 : "tscl"] ... and so on until the last name of the chain join query
Any help will be apreciated!
Your current expression does not consider the possible repetition of the from/join keywords.
You could try this expression :
(?<=from|join)\s+(\w+)
Please try it yourself here : https://regex101.com/r/qQ1rvs/1
The positive lookbehind used here (?<=...) allows to capture any words (\\w+) which are after it. In this case, every table names after these keywords are captured by this expression.
You could try this:
var query = 'select a.id, a.nama, b.alamat from tsql as a join tswl as b on a.id = b.id join tscl as c on a.id=c.id';
console.log(query.match(/(from|join)\s+(\w+)/g).map(e => e.split(' ')[1]))
Output:
Array(3) [ "tsql", "tswl", "tscl" ]
Note: The regexps discussed here will not work on most versions of MySQL because of the lame regexp parser.
Issues:
from
or join
being used in a comment or string or column name, etc. (I won't tackle this.) UNION
-- The table names will be lumped together without regard for such. FROM a JOIN(b JOIN c)
-- partially(?) Handled FROM ( SELECT ... )
-- not handled from
vs FROM
) -- outside the scipe of this regexp LEFT OUTER JOIN
-- not a problem So, this will mostly handle one table name:
\b(FROM|JOIN)\s+\`?(\w+)\`?
If JS has a way to iterate through a string, continuing where it left off, then you can get them all.
To better handle backtics and punctuation within:
\b(FROM|JOIN)\s+(\w+|`[^`]+`)
but you will need to strip the backtics when they occur.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.