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

156
Visualizações
JS split string on positive lookahead, avoid overlapping cases

I have a set of data that includes dated notes, all concatenated, as in the example below. Assume the date always comes at the beginning of its note. I'd like to split these into individual notes. I've used a positive lookahead so I can keep the delimiter (the date).

Here's what I'm doing:

  const notes = "[3/28- A note. 3/25- Another note. 3/24- More text. 10/19- further notes. [10/18- Some more text.]"
  const pattern = /(?=\d{1,2}\/\d{1,2}[- ]+)/g
  console.log(notes.split(pattern))

and the result is

[ '[',
  '3/28- A note. ',
  '3/25- Another note. ',
  '3/24- More text. ',
  '1',
  '0/19- further notes. [',
  '1',
  '0/18- Some more text.]' ]

The pattern \d{1,2} matches both 10/19 and 0/19 so it splits before both of those. Instead I'd like to have

[ '[',
  '3/28- A note. ',
  '3/25- Another note. ',
  '3/24- More text. ',
  '10/19- further notes. [',
  '10/18- Some more text.]' ]

(I can handle the extraneous brackets later.)

How can I accomplish this split with regex or any other technique?

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

To get your wanted output, you can prepend a word boundary in the lookahead, and you can omit the plus sign at the end of the pattern.

(?=\b\d{1,2}\/\d{1,2}[- ])

Regex demo

const notes = "[3/28- A note. 3/25- Another note. 3/24- More text. 10/19- further notes. [10/18- Some more text.]"
const pattern = /(?=\b\d{1,2}\/\d{1,2}[- ])/g
console.log(notes.split(pattern))

about 4 years ago · Santiago Gelvez Relatório

0

I would avoid split() here and instead use match():

var notes = "[3/28- A note. 3/25- Another note. 3/24- More text. 10/19- further notes. [10/18- Some more text.]";
var matches = notes.match(/\[?\d+\/\d+\s*-\s*.*?\.\]?/g);
console.log(matches);

You may do a further cleanup of leading/trailing brackets using regex, e.g.

var input = "[10/18- Some more text.]";
var output = input.replace(/^\[|\]$/, "");
about 4 years ago · Santiago Gelvez Relatório

0

Try .replaceAll() and this regex:

/(\[?\d{1,2}\/\d{1,2}\-.+?)/
// Replacement
"\n$1"

Figure I - Regex

Segment Description
(\[? Begin capture group - match literal "[" zero or one time
\d{1,2}\/ match a digit one or two times and a literal "/"
\d{1,2}\- match a digit one or two times and a literal "-"
.+?) match anything one to any number of times "lazily" - end capture group

Figure II - Replacement

Segment Description
\n New line
$1 Everything matched in the capture group (...)

const notes = "[3/28- A note. 3/25- Another note 3/24- More text. 10/19- further notes [10/18- Some more text.]";
const rgx = new RegExp(/(\[?\d{1,2}\/\d{1,2}\-.+?)/, 'g');
let result = notes.replaceAll(rgx, "\n$1");

console.log(result);

about 4 years ago · Santiago Gelvez 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