Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
Split text with regex in nodejs

I try to find sql script in a file to split in nodejs. Before split to text I add like -split- seperator with regex replace to begining of the sql script as the below:

SQL file:

/* 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 ')

Regex replace operation:

sqlText
.replace(/\s+create(\s+|global\s+|temporary\s+)table\s+/gi, `-split- CREATE $1 TABLE `)

Expected output:

/* 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 ')

But i get:

/* 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 ')

How can I exclude the query sentences in the comment line and quotes?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

  1. First split the text into lines .split(/\n/)
  2. Loop with map each line check if it line begins with create table, if so change it as you want -split- CREATE TABLE

let sqlText = `/* this is a comment for create table */
--this is another comment for create table

create table test1 (comment varchar);
create table test2 (comment varchar);

insert into text1 values('this is a comment for create table ')
`;
console.log(sqlText.split(/\n/).map(line=>line.replace(/^create\s+table\s+/gi, `-split- CREATE TABLE `)).join('\n'))

Notice: the ^ means "starts with".

UPDATE

more simple with RegExp flag m

console.log(sqlText.replace(/^create\s+table\s+/gmi, `-split- CREATE TABLE `))
                                                ☝️
about 4 years ago · Santiago Trujillo Relatório

0

Use

const str = `/* this is a comment for create table */
--this is another comment for create table

create table test1 (comment varchar);
create table test2 (comment varchar);

insert into text1 values('this is a comment for create table ')`
console.log(str.replace(/(\/\*[^]*?\*\/|^\s*--.*|'[^\\']*(?:\\[^][^\\']*)*'|"[^\\"]*(?:\\[^][^\\"]*)*")|[^\S\n\r]*create\s+table\s+/gmi, (_, x) => x || `-split- CREATE TABLE `))

We skip multiline comments (\/\*[^]*?\*\/), single line comments (^\s*--.*), single quote literals ('[^\\']*(?:\\[^][^\\']*)*') and double quote literals.

The [^\S\n\r]*create\s+table\s+ regex part finds matches outside of afore-mentioned patterns.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda