【发布时间】:2022-07-29 18:19:36
【问题描述】:
我尝试在文件中查找 sql 脚本以在 nodejs 中拆分。在拆分为文本之前,我添加了 -split- 分隔符和正则表达式替换到 sql 脚本的开头,如下所示:
SQL 文件:
/* this is a comment for create table */
--this is another comment for create table
create table test1 (comment varchar);
create temporary table test2 (comment varchar);
insert into text1 values('this is a comment for create table ')
正则表达式替换操作:
sqlText
.replace(/\s+create(\s+|global\s+|temporary\s+)table\s+/gi, `-split- CREATE $1 TABLE `)
预期输出:
/* this is a comment for create table */
--this is another comment for create table
-split- CREATE TABLE test1 (comment varchar);
-split- CREATE temporary TABLE test2 (comment varchar);
insert into text1 values('this is a comment for create table ')
但我明白了:
/* this is a comment for -split- CREATE TABLE */
--this is another comment for -split- CREATE TABLE
-split- CREATE TABLE test1 (comment varchar);
-split- CREATE temporary TABLE test2 (comment varchar);
insert into text1 values('this is a comment for -split- CREATE TABLE ')
如何排除注释行和引号中的查询语句?
【问题讨论】:
标签: javascript regex