Calling a function within ReReplace function - coldfusion

Is there a way to write in coldfusion something like:
< cfset ReReplace(value,"&#\d+;","#decodeHtmlEntity(\1)#", "all") >
Thanks a lot

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.

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.

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.

Related

ColdFusion looping through a list with text qualifiers

I'm trying to figure out a way to implement text qualifiers (surrounding quotes) in cfloop lists. cfloop list doesn't seem to have an option for ignoring text between certain characters. Of course I could just change the delimiter but I'm surprised I can't figure out a more elegant way to do this and now it's got me curious...
Here is the list I'm looping through:
Mockup_"/docs/R0001/Mockup_2014-07-10.pdf"_MDEXTER_2014-07-10
And of course this is the loop:
<cfloop list="#list#" delimiters="_" index="i">
#i#
</cfloop>
So it is treating
"/docs/R0001/Mockup
and
2014-07-10.pdf"
as separate elements whereas they need to form one URL. Any suggestions?
If the line of text is going to be relatively constant why not do this?
strList1 = listGetat('Mockup_"/docs/R0001/Mockup_2014-07-10.pdf"_MDEXTER_2014-07-10',2,'"');
writeDump(strList1);
The above will produce this without quotes: "/docs/R0001/Mockup_2014-07-10.pdf"
Again this assumes a standard format.

allow quotes in textfield coldfusion

I'm building a form through which users will be able to submit articles. my current regex allows only certain characters and works as it should, though I do not know how to allow quotes as well. here is the code
<cfif refind ("[^A-Z a-z 0-9\\+\-\?\!\.\,\(\)]+", trim(form.articleText)) and len (trim(form.articleText)) gte 15>
<cfset msg = "The article can not contain special characters.">
</cfif>
I tried using &quot as in c# but it does not work!
Add quotes in your character class:
<cfif refind ("[^A-Za-z0-9 +?!.,()\\""'-]+", trim(form.articleText)) ...
anubhava's answer gives you what you've asked for, but the solution you probably need is actually something completely different: to use the ESAPI encodeForX functions in CF10 to encode the output appropriately for its context, such as encodeForHtml, instead of trying to restrict what characters can be written and constantly having to update it.
At most, you might want something such as:
<cfif refind('[[:cntrl:]]',form.articleText) >
<cfset msg = "The article can not contain control characters.">
</cfif>
Which will block unprintable control characters, whilst not preventing perfectly reasonable characters such as accented letters, currency symbols, and so on.

Escaping + and % for email in Regular expression using ColdFusion

I am coding for Email validation which may take set of special characters. I could successfully add others to RegEx. However when I try for '+' and '%' it gives me an error.
I used '\' to allow special characters.
\+ --> adds a space removing + sign
\% --> removes 3rd char after % sign
ColdFusion has several built-in validation functions for things such as email addresses. You could simply use something like:
<cfif IsValid("email", YourEmailVar)>
<!--- do what you want for success here --->
<cfelse>
<!--- do what you want for validation failure here --->
</cfif>
Documentation for IsValid function
The IsValid function will also allow you to use RegEx if you prefer.
EDIT
In order to validate variables from the URL scope simply prepend that to the variable name. Like so:
<cfif IsValid("email", URL.YourURLEmailVar)>

How to get rid of last comma when generating a list?

I'm writing a web service in ColdFusion. The problem is that I cannot figure out how to get rid of the comma after the last element. My code looks like this:
<cfoutput query="Attachments">
#url#,
</cfoutput>
Which produces output like this (notice the trailing comma)
url1,url2,url3,
How can I get rid of the trailing comma and produce this instead?
url1,url2,url3
This is an easy method:
<cfoutput>#ValueList(Attachments.url)#</cfoutput>
Jake's answer is what's needed in this particular case.
For more generic cases, you can do this:
<cfloop ...>
<cfset myList=listAppend(myList,value)>
</cfloop>
There's also a bit of trickery you can do since ColdFusion (by default) ignores empty list elements:
<cfset myList=arrayToList(listToArray(myList))>
Heck, even this'll work:
<cfset myList=listChangeDelims(myList , "," , ",")>
Of course, if you're not outputting the list as a string, you don't need to worry about that comma on the end since ColdFusion will just ignore the empty element. If you are outputting it as a string, here's yet another way to clean up that comma. It's not as reliable as the others, though.
<cfoutput>#left(trim(mylist),len(trim(mylist))-1)#</cfoutput>

What do you use to render text from <textarea> to <div> with ColdFusion?

What do you use? Replace() linebreak chars with <br>? what about spaces? like maybe replace 2 spaces with ?
ParagraphFormat() sucks.
paragraphformat2()? http://www.cflib.org/udf.cfm/paragraphformat2
ReplaceNoCase(someString, "\n", "<br>","all")
One thing you may have to take into account is that different OS treat line breaks differently. Windows uses CR/LF while OS X and Unix use CR. I have used a code block effectively in the past to manage the different possibilities when it comes to reading in text files. Same principles could apply here. It's not 100% perfect, but on the rare occasions it has failed me it was because of an odd method of how the file was created. I modified it to fit the general idea of what you are going after.
<cfset variables.CRLF = findnocase(variables.textFromTextarea,"#chr(10)#") />
<cfif variables.CRLF>
<cfset variables.textFromTextarea = replaceNoCase(variables.textFromTextarea,"#chr(10)#","<br>","all") />
<cfelse>
<cfset variables.textFromTextarea = replaceNoCase(variables.textFromTextarea,"#chr(13)#","<br>","all") />
</cfif>
The idea here is that you are looking for the windows-only LF. If found, replace on it. Otherwise replace on the CR. Maybe that will work for you.
I needed to replace text from a textarea input to html when output on a webpage, and preserved the line break. So based on the accepted answer, I simply modified it to this, and it worked:
ReplaceNoCase(someString, Chr(10), "<br />","all")
Hope that helps anyone else.