用 rereplace 或 replace 替换字体

replacing the fonts with rereplace or replace

我需要一些关于 replace 或者 rereplace

的帮助

我正在尝试将 font-family:anything 替换为 font-family:swiss7

但是如果有一个值font-family: BebasNeue;我希望该字体保持不变并且不添加字体大小,但将字体大小添加到其他字体

我正在学习本教程,但不知何故它与我需要实现的目标不匹配

https://www.sitekickr.com/snippets/coldfusion/strip-css-styles

我会建议一种更易于维护的方法: if !findNoCase( styleString, 'BebasNeue'){ styleString = REReplace(style, 'font-family:[^"|^;]*', "font-family:swiss7; font-size:12", "ALL"); }

这应该可以满足您的需求:如果是 BebasNeue,请不要管它,否则,将字体系列更改为 swiss7 并添加字体大小。但它也没有复杂到正则表达式新手无法理解发生了什么。

更新:好吧,我认为这个问题更简单。如果您想查看我的原始答案,请查看此 post 的编辑历史记录。这个答案使用类似的原则,但您可以在其中搜索您不想删除的项目,然后将其推回替换。

<cfset teststr1 = "font-family: BebasNeue;" />
<cfset teststr2 = "font-family: Verdana" />
<cfset teststr3 = "font-family: BebasNeue; font-family: Times New Roman; color: red" />

<cfset search1 = "(font-family:\s*)((BebasNeue)|[\w ]+)(;)?" />
<cfset replace1 = "font-family: swiss7" />

<cfset search2 = "BebasNeueswiss7" />
<cfset replace2 = "BebasNeue" />

<cfoutput>
    <ol>
    <li>#replaceNoCase(reReplaceNoCase(teststr1, search1, replace1, "all"), search2, replace2, "all")#</li>
    <li>#replaceNoCase(reReplaceNoCase(teststr2, search1, replace1, "all"), search2, replace2, "all")#</li>
    <li>#replaceNoCase(reReplaceNoCase(teststr3, search1, replace1, "all"), search2, replace2, "all")#</li>
    </ol>
</cfoutput>

结果:

1. font-family: BebasNeue;
2. font-family: swiss7
3. font-family: BebasNeue; font-family: swiss7; color: red  

因此,基本上您将所有字体系列替换为所选字体类型,在本例中为 swiss7,但通过在替换中包含组选择器,您将 BebasNeue 字体保留在字符串中。然后再执行一个额外的步骤来清理留下的组合字体名称。