Figure out myself, which also works when a tag includes another tag. Need to run multiple passes if you want to remove content in diferent tags in one go.
void removeAllBetweenHtmlTag( QString& content, const QStringList& tagList )
{
arg( tagList.join( "|" ) );
QRegularExpression rx( tagStr, QRegularExpression::CaseInsensitiveOption );
while( true ) {
int len1 = content.length();
content.replace( rx, " " );
qDebug() << content;
int len2 = content.length();
if ( len1 == len2 ) {
break;
}
}
}
int main( int, char** argv )
{
qDebug() << "";
QString str1
( "This is a <div style='color:red;'>test</div> to <span>remove</span> all <div>divs containing <span>spans</span></div> from html string" );
qDebug() << str1;
removeAllBetweenHtmlTag
( str1,
QStringList() <<
"div" <<
"span" );
qDebug() << str1;
qDebug() << "";
}
void removeAllBetweenHtmlTag( QString& content, const QStringList& tagList )
{
QString tagStr = QString( "(<\\b(%1)\\b[^>]*>[^<]+</\\b(%1)\\b>)" ).
arg( tagList.join( "|" ) );
QRegularExpression rx( tagStr, QRegularExpression::CaseInsensitiveOption );
while( true ) {
int len1 = content.length();
content.replace( rx, " " );
qDebug() << content;
int len2 = content.length();
if ( len1 == len2 ) {
break;
}
}
}
int main( int, char** argv )
{
qDebug() << "";
QString str1( "This is a <div style='color:red;'>test</div> to <span>remove</span> all <div>divs containing <span>spans</span></div> from html string" );
qDebug() << str1;
removeAllBetweenHtmlTag( str1, QStringList() << "div" << "span" );
qDebug() << str1;
qDebug() << "";
}
To copy to clipboard, switch view to plain text mode
Bookmarks