How to capture only a specific part of a string?

Asked

Viewed 83 times

0

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

<txt_conarq_assunto></txt_conarq_assunto>

Example:

Entree:

<txt_conarq_assunto>A classificar</txt_conarq_assunto>

Exit:

Classify

3 answers

2

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]);

2


use the pattern <[tag]>(.*?)</[tag]> to get content between elements. Replace [tag] with the real element from which you want to extract 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); });

Browser other questions tagged

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