简体   繁体   中英

Regular Expression replace everything except a string

I am using regular expressions to replace any formatting within span tags using the following expression and it works.

retValue = System.Text.RegularExpressions.Regex.Replace(retValue, @"<span[^>]*>", "<span>");

Now, I would like to replace any formatting within the span tags except for 'Underline'. For example in the following string, I would like to remove the formatting within the second span tag, but keep the formatting of the first span tag.

 string retValue = "<p><span style=\"text-decoration: underline;\">Test Underline</span></P><p><span style="color:blue">blue</span></p>";

So my retValue should be :

retValue = "<p><span style=\"text-decoration: underline;\">Test Underline</span></P><p><span>blue</span></p>";

I tried using the following expression, but it doesn't replace anything at all. I am trying to understand what is wrong with this code and how can I achieve the expected result.

retValue = System.Text.RegularExpressions.Regex.Replace(retValue, @"<span[^style=\""text-decoration:underline;>]*>", "<span>");

You need to escape the special character correctly:

var pattern = "\\<span[^style\\=\\\"text\\-decoration\\:underline\\;\\>]*>";
retValue = System.Text.RegularExpressions.Regex.Replace(retValue, pattern, "<span>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM