Replace text within tag

Asked

Viewed 121 times

2

Am I trying to replace inside a tag? EX:

mystr = '<html><head></head><body><p>Aqui meu texto</p></body></html>'
mystr = mystr:gsub('<.+>', '')
print(mystr)

I’d like you to return:

Aqui meu texto

But nothing comes back.

1 answer

4


The problem is that '<.+>' house all the string and the gsub then removes all content.

If you want to extract a text, use string.match and catches:

print(mystr:match('<p>(.-)</p>'))

If you want to replace the text, use

mystr = mystr:gsub('<p>.-</p>','<p>Eis o novo texto</p>',1)
  • With mystr:gsub('<.->', ''), I was able to do what I was trying to do.

  • @Gabrielsales, ah, now I understand: you want remove all tags.

Browser other questions tagged

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