在 ReReplace 函数中调用函数

发布于 2024-11-07 13:58:28 字数 147 浏览 1 评论 0原文

有没有办法在 Coldfusion 中写类似:

< cfset ReReplace(value,"&#\d+;","#decodeHtmlEntity(\1)#", "all") >

非常感谢

Is there a way to write in coldfusion something like:

< cfset ReReplace(value,"&#\d+;","#decodeHtmlEntity(\1)#", "all") >

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

じ违心 2024-11-14 13:58:28

简短的回答是“否”。

CF 本身不处理正则表达式执行。它交给 Java 库(Oro、IIRC)来处理。这意味着您调用的任何 CF 函数都会在正则表达式之前执行。

有一个解决方法,尽管它不像能够传递函数那么优雅。使用 reFind() 来发现您要查找的所有实例,并一一重新定位它们。如果您从最后到第一个进行替换(例如,如果有 3 个实例,则执行第 3 个,然后是第 2 个,然后是第 1 个),每场比赛的起点将保持在同一位置,因此您可以重新查找所有,而不是在循环中进行重新查找。

HTH。

The short answer is "No".

CF doesn't handle the regular expression execution natively. It hands off to a Java library (Oro, IIRC) to handle that. This means that any CF functions you call get executed before toe regex.

There is a workaround, although it's not nearly as elegant as being able to pass functions would be. Use reFind() to discover all the instances of what you are looking for, and repolace them one-by-one. If you do the replaces last-to-first (eg if there are 3 instances, do the 3rd, then the 2nd, then the 1st) your starting point for each match will remain in the same location, so you can do an reFind all, instead of doing the reFind in the loop.

HTH.

空城缀染半城烟沙 2024-11-14 13:58:28

如果您想将正则表达式值替换为decodeHTMLEntity 的参数,我认为这不起作用。

更新:

   <cfset myVar = ReReplace("ABC123DEF","(\d+)",addOne('\1'), "all") >
   <cffunction name="addOne" access="public" output="false" returntype="string">
    <cfargument name="arg1" required="true" type="string" />
    <cfreturn arg1 + 1>
</cffunction>
<cfdump var="#myvar#">

上面的代码是为了从文本中查找 123 并在其中添加 1 而编写的,但这不起作用,因为 arg1 将具有 \1,它不是数值。

I don't think this will work if you want to replace regular expression value as argument of decodeHTMLEntity.

Updated:

   <cfset myVar = ReReplace("ABC123DEF","(\d+)",addOne('\1'), "all") >
   <cffunction name="addOne" access="public" output="false" returntype="string">
    <cfargument name="arg1" required="true" type="string" />
    <cfreturn arg1 + 1>
</cffunction>
<cfdump var="#myvar#">

Above code written to find 123 from text and add one into it but this will not work as arg1 will have \1 which is not numeric value.

安人多梦 2024-11-14 13:58:28

您是否尝试过简单地使用 URLDecode(value) ?

或者,如果您特别只想解码数字 html 代码,那么

<cfset myVar = ReReplace(value,"(&##[\d+];)",urlDecode('\1'), "all") >

将执行您需要的操作。

为了解释它在做什么:

  • 我已经用 CFML 版本替换了 PHPdecodeHTMLEntity 函数。
  • 如果要使用反向引用,则需要在正则表达式模式中指定捕获组。
  • 你需要将这些#加倍以转义它们,否则CF将寻找它永远找不到的接近的#。

Have you tried simply using URLDecode(value)?

Or if you specifically only want to decode the numeric html codes, then

<cfset myVar = ReReplace(value,"(&##[\d+];)",urlDecode('\1'), "all") >

will do what you need.

To explain what it is doing :

  • I've replaced the PHP decodeHTMLEntity function with the CFML version.
  • If you want to use back references you need to specify the capture groups in the regex pattern.
  • you need to double up those #'s to escape them, otherwise CF will be looking for a close # that it will never find.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文