How to capture only a specific part of a string?

0

Using regular expression, I would like to capture only the value inside a tag:

<txt_conarq_assunto></txt_conarq_assunto>

Example:

Entry:

<txt_conarq_assunto>A classificar</txt_conarq_assunto>

Output:

Sorting

    
asked by anonymous 09.05.2018 / 18:33

3 answers

2

use the <[tag]>(.*?)</[tag]> pattern to get content between elements. Replace [tag] with the actual element from which you want to extract the content

var texto = "<txt_conarq_assunto>A classificar</txt_conarq_assunto>";

texto.replace(/<txt_conarq_assunto>(.*?)<\/txt_conarq_assunto>/g, function(match, g1) { console.log(g1); });
    
09.05.2018 / 19:47
2

You can use this expression:

(?s)(?<=\<txt_conarq_assunto>)(.*?)(?=\<\/txt_conarq_assunto\>)

Functional example: link

    
09.05.2018 / 18:45
2

You can use /<txt_conarq_assunto>(.+?)<\/txt_conarq_assunto>/
See the example below, with javascript

var texto = "<txt_conarq_assunto>A classificar</txt_conarq_assunto>";
var m = texto.match( /<txt_conarq_assunto>(.+?)<\/txt_conarq_assunto>/ );
console.log(m[1]);
    
09.05.2018 / 18:50