Regular Expression REGEX HELP

Asked

Viewed 133 times

0

I have an array on the server in Nodejs, I am going through html files and I need to return the value that is in the middle of the span tag using a regular expression as it would look ?

{
< span class="filteredAds"> de teste< /span>,

< span class="filteredAds"> de teste23< /span>>

}

It is not known what may come in the middle of the tag after the.

Somebody give me a hand ?

  • 1

    Can’t take by class, id or tag ?

  • Avoid regex to process HTML. Regular expressions are not a good tool for this. You should consider using some extension to do this for yourself (jsdom) and, as @Magichat suggested, capture the elements by class or tag.

  • When I was performing the task I was in the back end, not the front end.

1 answer

4

Suggestion:

const regex = /<[^\/]*span[^>]*>[^<]+</g;
const subRegex = />([^<]+)</;
const string = `{
< span class="filteredAds"> de teste< /span>,

< span class="filteredAds"> de teste23< /span>>

}`;
const conteudo = string.match(regex).map(str => str.match(/>([^<]+)</).pop().trim());
console.log(conteudo); // ["de teste","de teste23"]

The idea is to divide into two steps: capture each span, extract the content. When I use [^<] in regex that means: any character, except <.

Browser other questions tagged

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