首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Regex清除font-size标签

使用Regex清除font-size标签
EN

Stack Overflow用户
提问于 2012-09-12 21:52:59
回答 2查看 2.8K关注 0票数 2

我尝试使用正则表达式将<font style="font-size:85%;font-family:arial,sans-serif">

代码语言:javascript
复制
font-size:85%;

我的正则表达式是^font-size:(*);

我的意思是我必须完全删除font-size标签。

有谁能帮帮我吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-12 22:06:10

当前正则表达式的几个方面会导致它失败:

代码语言:javascript
复制
^font-size:(*);

您将锚定到行^的开头-属性不在行的开头。

*本身没有任何意义。

将其更改为:

代码语言:javascript
复制
font-size: ?\d{1,2}%;
票数 4
EN

Stack Overflow用户

发布于 2012-09-12 22:06:01

这是您需要的正则表达式:

代码语言:javascript
复制
string html = @"<font style=""font-size:85%;font-family:arial,sans-serif"">";
string pattern = @"font-size\s*?:.*?(;|(?=""|'|;))";
string cleanedHtml = Regex.Replace(html, pattern, string.Empty);

即使在ptem中定义了font-size,或者定义了一组不同的样式(即,.未指定font-family )。您可以查看结果。

正则表达式的解释如下:

代码语言:javascript
复制
// font-size\s*?:.*?(;|(?="|'|;))
// 
// Match the characters “font-size” literally «font-size»
// Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*?»
//    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the character “:” literally «:»
// Match any single character that is not a line break character «.*?»
//    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the regular expression below and capture its match into backreference number 1 «(;|(?="|'|;))»
//    Match either the regular expression below (attempting the next alternative only if this one fails) «;»
//       Match the character “;” literally «;»
//    Or match regular expression number 2 below (the entire group fails if this one fails to match) «(?="|'|;)»
//       Assert that the regex below can be matched, starting at this position (positive lookahead) «(?="|'|;)»
//          Match either the regular expression below (attempting the next alternative only if this one fails) «"»
//             Match the character “"” literally «"»
//          Or match regular expression number 2 below (attempting the next alternative only if this one fails) «'»
//             Match the character “'” literally «'»
//          Or match regular expression number 3 below (the entire group fails if this one fails to match) «;»
//             Match the character “;” literally «;»
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12389807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档