Split whole text into javascript pieces

Asked

Viewed 101 times

2

Good afternoon, stackoverflow guys

I need to share the text I get from an ajax return. That my return has the following pattern

It starts with what will be value, the rest will be the content of an option, ending in the queue or available vacancies, as follows the example below.

222/05/2017 a 26/05/2017 em Belo Horizonte - PaperCut MF Técnico Presencial (34 hrs) - Fila de espera307/08/2017 a 11/08/2017 em Belo Horizonte - PaperCut MF Técnico Presencial (34 hrs) - Vagas disponíveis

How can I do this division... I have no ideas, I just need the logical reasoning for it...

  • Please avoid long discussions in the comments; your talk was moved to the chat

1 answer

2


Though I think it best that this return be a array, I managed to separate that way:

var response = "222/05/2017 a 26/05/2017 em Belo Horizonte - PaperCut MF Técnico Presencial (34 hrs) - Fila de espera307/08/2017 a 11/08/2017 em Belo Horizonte - PaperCut MF Técnico Presencial (34 hrs) - Vagas disponíveis";

var data = response.split(/(\d+)(?=(?:\d{2}\/\d{2}\/\d{4}\sa))/);

var obj = {};
for(var i = 1; i < data.length; i++) {
  obj[data[i]] = data[++i];  
}

console.log(obj);

Thus the variable obj will receive the id as key and the corresponding text as value.

  • 1

    Thank you very much my dear

Browser other questions tagged

You are not signed in. Login or sign up in order to post.