Guy I have a list of string.
盖伊我有一个字符串列表。
Select
id AS "cusId ",
name as 'cusName', gendar as ' Gendar.',
isPaid as " is'Paid ", total, remarks FROM
I need a regex that returns:
我需要一个返回的正则表达式:
Select
id
name
gendar
isPaid
total
remarks
FROM
And also ignore comma and 'AS' keyword.
并且还忽略逗号和'AS'关键字。
So far from PHP I can use preg_match_all('/(?<![\S"])([^"\'\s]+)(?![\S"])/')
and filter all query keywords later on, but came to JavaScript there is no lookbehind in regex.
到目前为止,我可以使用preg_match_all('/(?
1 个解决方案
#1
2
DISCLAIMER: The solution below is by no means a generic solution for parsing arbitrary SQL queries. To parse arbitrary SQL queries, you need to build or use an existing one. See also How to parse / tokenize an SQL statement in Node.js.
免责声明:下面的解决方案绝不是解析任意SQL查询的通用解决方案。要解析任意SQL查询,您需要构建或使用现有SQL查询。另请参见如何在Node.js中解析/标记化SQL语句。
So, taking into account your specific input strings, you can use a regex that will match what you do not need, and then will capture what you need:
因此,考虑到您的特定输入字符串,您可以使用与您不需要的匹配的正则表达式,然后将捕获您需要的内容:
/"[^"]*"|'[^']*'|\s+AS\s+|\s*((?:(?!\sAS\s)[^,\s])+)/gi
See the regex demo
请参阅正则表达式演示
Explanation:
-
"[^"]*"
- match a double-quoted substring that has no"
inside (replace with"[^"\\]*(?:\\.[^"\\]*)*"
if you need to support escaped"
inside) -
|
- or -
'[^']*'
- match single-quoted substring having no'
inside (replace with'[^'\\]*(?:\\.[^'\\]*)*'
if you need to support escaped'
inside) -
|
- or -
\s+AS\s+
- "AS" word inside 1+ whitespaces -
|
- or -
\s*
- 0+ whitespaces -
((?:(?!\sAS\s)[^,\s])+)
- Group 1 capturing one or more symbols other than,
and whitespace (see[^,\s])+
) that are not starting a sequence of a whitespace +AS
+ whitespace. It matches any text that is notspace
+AS
+space
.
“[^”] *“ - 匹配一个没有”内部的双引号子字符串(替换为“[^”\\] *(?:\\。[^“\\] *)*”如果你需要支持逃脱“内部”
| - 要么
'[^'] *' - 匹配没有'内部的单引号子字符串(替换为'[^'\\] *(?:\\。[^'\\] *)*'如果需要支持转义'里面)
| - 要么
\ s + AS \ s + - 1+空格内的“AS”字
| - 要么
\ s * - 0+空格
((?:(?!\ sAS \ s)[^,\ s])+) - 第1组捕获一个或多个符号,以及未启动的空白(见[^,\ s])+)空白序列+ AS +空格。它匹配任何非空格+ AS +空格的文本。
JS demo:
var re = /"[^"]*"|'[^']*'|\s+AS\s+|\s*((?:(?!\sAS\s)[^,\s])+)/gi;
var str = 'Select id AS "cusId ", name as \'cusName\', gendar as \' Gendar.\', isPaid as " is\'Paid " total , datetime FROM';
var res = [];
while ((m = re.exec(str)) !== null) {
if (m[1]) {
res.push(m[1]); // Add the Capture group 1 to the resulting array
}
}
document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>";
#1
2
DISCLAIMER: The solution below is by no means a generic solution for parsing arbitrary SQL queries. To parse arbitrary SQL queries, you need to build or use an existing one. See also How to parse / tokenize an SQL statement in Node.js.
免责声明:下面的解决方案绝不是解析任意SQL查询的通用解决方案。要解析任意SQL查询,您需要构建或使用现有SQL查询。另请参见如何在Node.js中解析/标记化SQL语句。
So, taking into account your specific input strings, you can use a regex that will match what you do not need, and then will capture what you need:
因此,考虑到您的特定输入字符串,您可以使用与您不需要的匹配的正则表达式,然后将捕获您需要的内容:
/"[^"]*"|'[^']*'|\s+AS\s+|\s*((?:(?!\sAS\s)[^,\s])+)/gi
See the regex demo
请参阅正则表达式演示
Explanation:
-
"[^"]*"
- match a double-quoted substring that has no"
inside (replace with"[^"\\]*(?:\\.[^"\\]*)*"
if you need to support escaped"
inside) -
|
- or -
'[^']*'
- match single-quoted substring having no'
inside (replace with'[^'\\]*(?:\\.[^'\\]*)*'
if you need to support escaped'
inside) -
|
- or -
\s+AS\s+
- "AS" word inside 1+ whitespaces -
|
- or -
\s*
- 0+ whitespaces -
((?:(?!\sAS\s)[^,\s])+)
- Group 1 capturing one or more symbols other than,
and whitespace (see[^,\s])+
) that are not starting a sequence of a whitespace +AS
+ whitespace. It matches any text that is notspace
+AS
+space
.
“[^”] *“ - 匹配一个没有”内部的双引号子字符串(替换为“[^”\\] *(?:\\。[^“\\] *)*”如果你需要支持逃脱“内部”
| - 要么
'[^'] *' - 匹配没有'内部的单引号子字符串(替换为'[^'\\] *(?:\\。[^'\\] *)*'如果需要支持转义'里面)
| - 要么
\ s + AS \ s + - 1+空格内的“AS”字
| - 要么
\ s * - 0+空格
((?:(?!\ sAS \ s)[^,\ s])+) - 第1组捕获一个或多个符号,以及未启动的空白(见[^,\ s])+)空白序列+ AS +空格。它匹配任何非空格+ AS +空格的文本。
JS demo:
var re = /"[^"]*"|'[^']*'|\s+AS\s+|\s*((?:(?!\sAS\s)[^,\s])+)/gi;
var str = 'Select id AS "cusId ", name as \'cusName\', gendar as \' Gendar.\', isPaid as " is\'Paid " total , datetime FROM';
var res = [];
while ((m = re.exec(str)) !== null) {
if (m[1]) {
res.push(m[1]); // Add the Capture group 1 to the resulting array
}
}
document.body.innerHTML = "<pre>" + JSON.stringify(res, 0, 4) + "</pre>";