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

136
Visualizações
Split String from substring for first match

Below is an example of a String that I want to split it to 3 parts from a substring

test01String001s

Examples:

  • example_1 subString == "0"

    res: [ "test","0","1String001s" ] - test01String001s

  • example_2 subString == "st"

    res: [ "te","st","01String001s" ] - test01String001s

  • example_3 subString == "01s"

    res: [ "test01String0","01s", "" ] - test01String001s

  • example_4 subString == "t"

    res: [ "","t","est01String001s", ] - test01String001s

I try to resolve it via

 var string='test01String001s'
 var keyword="01"
 console.log(string.split(keyword));

output:

     ["test","String0","s"]  instead of [ "test","01","String001s"]

or

 var string='test01String001s'
 var keyword="0"
 console.log(string .split(keyword, 2));

output

 ["test","1String"]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

To get 3 parts I wouldn't use split, but use some logic

const mystring = 'test01String001s'
const sp = ['01','1','t','st', '1s']

sp.forEach(item=>{
  let idx=mystring.indexOf(item)
  let result=[]
  if(idx===0){
    result.push('')
    result.push(item)
    result.push(mystring.slice(item.length))
  }else if (idx+item.length ==mystring.length){
    result.push(mystring.slice(0,idx))
    result.push(item)
    result.push('')
  }else if(idx!=-1){
    result.push(mystring.slice(0,idx))
    result.push(item)
    result.push(mystring.slice(idx+item.length))
  }
  console.log(result)
})

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this.

var string = "test01String001s";
var splitKey = '01';
var components = string.split(splitKey);
var array = ([components.shift(), components.join(splitKey)]);
array.splice(1, 0, splitKey);
console.log(array.join());

Output: test,01,String001s

about 4 years ago · Juan Pablo Isaza Relatório

0

This should work:

const test = 'test01String001s';
const key = "t";
const re = new RegExp('(.*?)(' + key + ')(.*)');
const result = test.match(re, "regex");
result.shift();

console.log(result);

So this regex match is returning an array of:

(.*?) - anything before key

(' + key + ') - key

(.*) - anything after key

Unfortunately it also returns the whole matched word, so I used shift to remove first match from array.

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