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

137
Visualizações
Splitting a string like "__f__g_f_" Javascript

Say I have a string __f__g_f_

How can I achieve splitting it up to an array like ['__','f','__','g','_','f','_']

almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This should do:

const str = '__f__g_f_'

const parts = str.split(/([a-z]+)/)

console.log(parts)

almost 4 years ago · Santiago Trujillo Relatório

0

Instead of using .split() you can use .match() with a matching group in the regex, it will give you an array of all your wanted matches.

const str = '__f__g_f_'
const parts = str.match(/(_+)|([a-z]+)/g)
console.log(parts);
[
  "__",
  "f",
  "__",
  "g",
  "_",
  "f",
  "_"
]

almost 4 years ago · Santiago Trujillo Relatório

0

Split works at O(N) time complexity so you can write your own implementation and it is more readable and more maintainable

const s = "__f__g_f_"
const array = []

let buffer = ""
for (let i = 0; i < s.length; i++) {

    if (s[i] === "_") {
        buffer += s[i]
        continue
    }

    if (buffer.length > 0) {
        array.push(buffer)
        buffer = ""
    }
    array.push(s[i])
}
if (buffer.length > 0) {
    array.push(buffer)
}
console.log(array)

Edit:

(/([a-z]+)/) is elegant but always be in the shoes of other developers. What does (/([a-z]+)/) mean at a glance ? Regex is like a compiled program, machine code. unless all developers are adept in regex, They are not self-documented. They require to be commented on. And there might have bugs in them, how can you trust /([a-z]+)/ if it actually works without running the code? Seeing human readable code is always preferable. And finally, you may be surprised that regex can have weird time complexity, sometimes they jump to exponential. You don't know how they are implemented. Here, a simple plain and readable implementation is 100% faster than the regex.

console.time('regex');
for (let i = 0; i < 100_000_000; i++) {
  const str = '__f__g_f_'
  const parts = str.split(/([a-z]+)/)
}
console.timeEnd("regex")


console.time('plain_fast');

for (let i = 0; i < 100_000_000; i++) {
  const s = "__f__g_f_"
  const array = []

  let buffer = ""
  for (let i = 0; i < s.length; i++) {

    if (s[i] === "_") {
      buffer += s[i]
      continue
    }

    if (buffer.length > 0) {
      array.push(buffer)
      buffer = ""
    }
    array.push(s[i])
  }
  if (buffer.length > 0) {
    array.push(buffer)
  }
}
console.timeEnd("plain_fast")

regex: 10540.700ms
plain_fast: 4556.500ms
almost 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