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

244
Visualizações
How to split string by Regex matching in JavaScript

I am already trying for over an hour and cant figure out the right way to do it. I have this Regex:

const search = /("Response)\s+\d+\s+(of)\s+\d+(":)\s+/g;

I have this string:

{"Response 1 of 2": {"Name": "Jouny", "Class": "TR23", "Message Type": "Offer", "ID Identifier": "19256", "Address": "hjfhfgjhjhj"}, "Response 2 of 2": {"Name": "Sarah", "Class": "BHT56", "Message Type": "Alarm", "ID Identifier": "89756767", "Address": "oplkdhdggd"}}

I would like to have a result like this:

[
   {"Name": "Jouny", "Class": "TR23", "Message Type": "Offer", "ID Identifier": "19256", 
        "Address": "hjfhfgjhjhj"},
   {"Name": "Sarah", "Class": "BHT56", "Message Type": "Alarm", "ID Identifier": "89756767", 
        "Address": "oplkdhdggd"}
 ]

my code:

var lines = results.toString().split(/\n/);
lines.forEach(function (item) {
    if (item.indexOf(myData) > 0) {
        var json = item.slice(item.indexOf(script) + script.length + 2).replace(PRINTABLE_CHAR, '');
        var jsonMatch = json.split(json.match(search));
        var result = jsonMatch.substr(0, jsonMatch.lastIndexOf('}')).replace(PRINTABLE_CHAR, '');
        try {
                data = JSON.parse(result);
            } catch (e) {
                console.log('ERROR');
            }
    
        }
    });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can achieve the same result using Object.values

const initialObject = {"Response 1 of 2": {"Name": "Jouny", "Class": "TR23", "Message Type": "Offer", "ID Identifier": "19256", "Address": "hjfhfgjhjhj"}, "Response 2 of 2": {"Name": "Sarah", "Class": "BHT56", "Message Type": "Alarm", "ID Identifier": "89756767", "Address": "oplkdhdggd"}}


const resultObject = Object.values(initialObject);

console.log(resultObject);

about 4 years ago · Juan Pablo Isaza Relatório

0

This issue can be solved without regex. You can parse the first string as JSON using JSON.parse(string), then you can get the list of keys using Object.keys(json) and then do a forEach and push it to a list, which will get you what you want without having to deal with regex. I will show the code below

let string = '{"Response 1 of 2": {"Name": "Jouny", "Class": "TR23", "Message Type": "Offer", "ID Identifier": "19256", "Address": "hjfhfgjhjhj"}, "Response 2 of 2": {"Name": "Sarah", "Class": "BHT56", "Message Type": "Alarm", "ID Identifier": "89756767", "Address": "oplkdhdggd"}}'
let list = [] //this will be the list that we will return at the end
let data = JSON.parse(string) //parse the data into json

Object.keys(data).forEach(key => list.push(data[key])) //get the keys and iterate through it
return list //return the list

This should be a much better alternative to using regex. Regex was not made for what you are trying to do.

EDIT: Ah, follow @callback 's advice. Using object.values() would be a much better solution.

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