How to pick up part of the string from the size of a word?

Asked

Viewed 117 times

0

I want to take only the name of the discipline of an H1 containing the following text: "ENF N1A METHODOLOGY OF SCIENTIFIC RESEARCH (MES)". In this case what interests me in this string is the value "CINENTÍFICA RESEARCH METHODOLOGY". The idea I had was to use regex to return this value by taking text from the string set that was larger than x. Someone could tell if this is the best way?

[ADD]
The full names of the disciplines follow the following pattern:

[cod_dis)][""][class][" "][discipline][" "][(cod_branch)]

In this case, for the examples cited I marked in italics the part I would like to take from the whole:

ENF N1A METHODOLOGY OF SCIENTIFIC RESEARCH (MES)
ENF N1A APPLIED INFORMATICS (MES)
ENF N1A INSTRUMENTAL ENGLISH (MES)

Would look like this:

METHODOLOGY OF SCIENTIFIC RESEARCH
APPLIED INFORMATICS
INSTRUMENTAL ENGLISH

P.S. I know that before the discipline there is the class code that has at most 4 characters, and after the discipline, there is the (cod_filial) that always stays in parentheses.

  • 1

    Only METODOLOGIA DA PESQUISA CIENTÍFICA? Or some other rule for a list of names and that has some pattern?

  • 1

    That example has become a bit vague. I suggest giving more examples of more catches you want to make, to make clearer the rule you are applying

  • I would like to use this same rule to find the name of the other disciplines. I edited the question adding more details. Obg.

  • The regex I made is very similar to the answer, see the demo on Regex101. I will not post because the answer is already very good.

  • I’ll look yes. Thanks a lot @danieltakeshi.

1 answer

4


Based on the reported standards, a regex that can be used is the /([^ ]{1,4} [^ ]{1,4} )([^(]+)( \([^)]+\))/g. Example:

var text = [
  'ENF N1A METODOLOGIA DA PESQUISA CIENTÍFICA (MES)',
  'ENF N1A INFORMÁTICA APLICADA (MES)',
  'ENF N1A INGLÊS INSTRUMENTAL (MES)'
];

var subtext = [];

for(var i = 0; i < text.length; i++) {
  var exec = /([^ ]{1,4} [^ ]{1,4} )([^(]+)( \([^)]+\))/g.exec(text[i]);
  subtext.push(exec ? exec[2] : null);
}

var $output = document.getElementById("output");

for(var i = 0; i < text.length; i++) {
  var $h1 = document.createElement('h2');
  $h1.innerHTML = text[i];
  $output.append($h1);
}

for(var i = 0; i < text.length; i++) {
  var p = document.createElement('p');
  p.innerHTML = subtext[i];
  $output.append(p);
}
<div id="output"></div>

  • Damn young! I even wanted to cry. That’s what I was looking for. Thank you so much @Diego

Browser other questions tagged

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