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

120
Visualizações
Split string by first delimiter found

I have a string with multiple delimiters and I wanted to split it by the first delimiter found. I couldn't find any method to do this every method I tried will return either all the char found after the last delimiter or the char between the first 2 delimiters found. How can I get all char after the first delimiter? Any help is appreciated. Thanks in advance.

x = 'aa-bb-cc-dd'
console.log(x.split('-')[1]) // returns bb expected bb-cc-dd

console.log(x.split('-').pop()) // returns dd expected bb-cc-dd

console.log(x.split('-')[1].pop()) // something like this but obviously this wont work

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use String#indexOf with String#slice like this:

x = 'aa-bb-cc-dd'
console.log(x.slice(x.indexOf('-')+1)) // returns bb-cc-dd

about 4 years ago · Juan Pablo Isaza Relatório

0

One way :

x = 'aa-bb-cc-dd'
function splitOnFirstDelimiter (str, del) {
  const index = str.indexOf(del);
  return index < 0 ? str : str.slice(index + del.length);
}
console.log(splitOnFirstDelimiter(x, '-'))

about 4 years ago · Juan Pablo Isaza Relatório

0

How about using slice instead of split and then create a function which will return the expected value like this?

function splitting (str) {
    return str.slice(str.indexOf('-') + 1)
}

const r1 = splitting('aa-bb-cc-dd') // bb-cc-dd
const r2 = splitting(r1) // cc-dd
const r3 = splitting(r2) // dd

console.log(r1)
console.log(r2)
console.log(r3)

about 4 years ago · Juan Pablo Isaza 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