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

228
Visualizações
¿Cómo haría para dividir una cadena en caracteres? Javascript

Quiero dividir una cadena, como esta:

 "Hello $id1, how are you $id2 etc...",

pero "$idInt" no debe dividirse:

 const test = [ "$id1", "$id2", "$id3" ] let str = "test $id1!"; str = str.split(""); // wanted result: ["t", "e", "s", "t", " ", "$id1", "!"]; // actual result: ["t", "e", "s", "t", " ", "$", "i", "d", "1" "!"]

Esto tomaría cualquier cosa de la matriz de "prueba" y no la dividiría con cada carácter separado.

Solo busqué otros recursos, pero no he podido encontrar algo que haga exactamente lo que quiero que haga.

También tengo este código:

 let reg = /\%\[a-zA-Z0-9]\:[0-9]+\%/gim; let yourstring = 'Some thinger %09ff:13%'; let matches = reg.exec(yourstring); let records = []; let pieces = []; if (matches != null) { for (let [i, match] of Array.from(matches).entries()) { let start = yourstring.indexOf(match); let end = match.length + start; records.push({ start: start, end: end, match: match, }); } records.sort((a, b) => (a.start > b.start ? 1 : a.start < b.start ? -1 : 0)); if (records[0].start > 0) { records = [{ start: 0, end: 0, match: "" }].concat(records); } for (let i = 1; i < records.length; i++) { pieces.push(yourstring.slice(records[i - 1].end, records[i].start).replace(......)); // replace goes here pieces.push(records[i].match); } yourstring = pieces.join("") } else { yourstring = yourstring.replace(.......) // replace goes here }

pero es regex y quiero que no se reemplace un conjunto de cadenas en lugar de esto aquí con regex

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

0

Yo propondría usar match en lugar de split :

 let s = "Hello %id:1%, how are you %id:2% etc..."; let result = s.match(/%id:\d+%|./gs); console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Deberías usar la función de división así:

 let str1 = "test %id:1%!" let arr1 = str1.split('%') let test = []; // arr1 will become ['test ','id:1','!'] // every alternate one will become one of the usable one. arr1.forEach((x,index)=>{ if (index%2 == 1) { // push in test and add the '%'s back in. test.push(`%${x}%`) } })
about 4 years ago · Juan Pablo Isaza Relatório

0

La respuesta de Trincot es mejor, pero ya escribí esto, así que aquí está ...

 var str = "Hello %id:1%, how are you %id:2% etc..."; console.log(splitStr(str)); function splitStr(str) { var is_var = false; var chars = []; var group = ''; for (var i = 0; i < str.length; i++) { let char = str[i]; if (is_var) { group += char; if ('%' == char) { chars.push(group); group = ''; is_var = false; } } else { if ('%' == char) { group += char; is_var = true; }else{ chars.push(char); } } } return chars; }

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