Regex to separate a string

1

Galera I need to separate a piece of a string to put as a result of a specific class.

I have the following return

02/10/2017 a 05/10/2017 em São Paulo - Papercut MF Técnico Presencial (28 hrs) - Vagas disponíveis

I needed some regex to separate in the text in the first occurrence of a, always in the first occurrence of a, which is between the dates.

Thank you in advance

    
asked by anonymous 12.09.2017 / 20:48

2 answers

1

You can use the following regular expression:

/([0-9]{2}\/[0-9]{2}\/[0-9]{4}) a ([0-9]{2}\/[0-9]{2}\/[0-9]{4})/g

This expression will look for 2 numbers, followed by slash, followed by 2 more numbers, followed by slash again and soon after, plus 4 numbers. After this you will search for String a and the same combination.

var texto = "02/10/2017 a 05/10/2017 em São Paulo - Papercut MF Técnico Presencial (28 hrs) - Vagas disponíveis";

console.log(separar(texto));

function separar(texto) {
  var regex = /([0-9]{2}\/[0-9]{2}\/[0-9]{4}) a ([0-9]{2}\/[0-9]{2}\/[0-9]{4})/g;
  var resultado = [];
  var combinacao = regex.exec(texto);
  
  resultado.push(combinacao[1]);
  resultado.push(combinacao[2]);
  
  return resultado;
}
    
12.09.2017 / 21:10
0

I understand that you want to get a part of a string then ...

You can transform the string into an array by separating elements by spaces:

var teste = "02/10/2017 a 05/10/2017";
var arrayTeste = teste.split(' ');

This will return an array with 3 elements:

0- 02/10/2017

1- a

2-5 / 10/2017

You can get them separately:

arrayTeste[0];
arrayTeste[1];
arrayTeste[2];

If you want you can still use split ('/') to separate the day, month and year

Comments: The first element has the 0 position, the second the 1 position, and so on

Elements will no longer have the space () character because split clears it to separate elements from the array

I recommend you watch videos of rodrigo branas - link it has videos about js, angularjs and nodejs

    
12.09.2017 / 21:19