|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Calling a function on a regex backreference
Hopefully this should be an easy and quick answer.Ryan Mitchell 11/02/06 06:01 A On 11/2/06, Ryan Mitchell <ryan@rt.to> wrote:Rob Wilkerson 11/02/06 06:16 A hmm, see that wouldn't work.Ryan Mitchell 11/02/06 07:21 A On 11/2/06, Ryan Mitchell <ryan@rt.to> wrote:Rob Wilkerson 11/02/06 07:41 A ok, so i have a long string with emails within it, what i want to do is identify the emails in tehRyan Mitchell 11/02/06 08:02 A Peter is right. Now that I see what you're trying to do, I don'tRob Wilkerson 11/02/06 08:37 A >>im looping through a string, replacing emails with anClaude Schneegans 11/02/06 11:41 A ... I forgot to mention this to put a mailto in your code, use:Claude Schneegans 11/02/06 11:50 A It will work on the whole string, not just the back reference.Peter Boughton 11/02/06 07:49 A
Author: Ryan Mitchell
Hopefully this should be an easy and quick answer. I want to call a function on a regex backreference, ie: REreplace(string,regex,function('\1'),'ALL'); is this possible? I've tried every imaginable combination to make it work, and its just not happening!!! Thanks, Ryan
Author: Rob Wilkerson
On 11/2/06, Ryan Mitchell <ryan@rt.to> wrote: > Hopefully this should be an easy and quick answer. > > I want to call a function on a regex backreference, ie: > > REreplace(string,regex,function('\1'),'ALL'); > > is this possible? I've tried every imaginable combination to make it work, and its just not happening!!! I don't think you can do that directly. You can, however, do something like this: <cfset backref = REReplace ( string, regex, '\1', 'ALL' ) /> <cfset out = function ( backref ) /> An extra step, but it will work.
Author: Ryan Mitchell
hmm, see that wouldn't work. im looping through a string, replacing emails with an obfuscated/hidden version of the email address. .. so i need to call the function directly...
Author: Rob Wilkerson
On 11/2/06, Ryan Mitchell <ryan@rt.to> wrote: > hmm, see that wouldn't work. > im looping through a string, replacing emails with an obfuscated/hidden version of the email address... so i need to call the function directly... If context is important, try posting a larger snippet. Maybe that will help.
Author: Ryan Mitchell
ok, so i have a long string with emails within it, what i want to do is identify the emails in teh string, and replace them with the result of the function obfuscate(email)... so what im doing is this: function hideEmails(s) { return rereplace(s,"(mailto:)([\w.]+@[\w.]+)",obfuscate("\1\2"),'ALL'); } however, all this does is return an encrypted version of the string "\1\2", instead of the backreferences.
Author: Rob Wilkerson
Peter is right. Now that I see what you're trying to do, I don't think it can be done in CF. I'm don't think it can be done in Java, either. To perform multiple actions within a single string (which you're doing since you're assuming multiple matches), you'll need to use REFind() and loop over each match. <cfscript> s = yourString; start = 1; re = 'mailto:[\w.]+@[\w.]+'; while ( start lt len ( s ) ) { curMatch = REFindNoCase ( re, str, start, true ); if ( curMatch.pos[1] neq 0 ) { /** * call obfuscate() on each match * */ } start = curMatch.pos[1] + curMatch.len[1]; </cfscript> And, while I'm here, you're mailto string is missing a few valid characters in email addresses. Most commonly, the "-", but others are also valid - if rarely used. If you're interested in RFC-compliance then use [_A-Za-z0-9!##$%&'*+/=?^`{|}~-]+ instead of [\w.]+. Looks weird, I know, but the RFC says those characters are all legal in an email address and I've got customers who have "&" in their addresses. That's how I found out that the characters are legal (I'm not prone to perusing RFCs just for grins). :-) On 11/2/06, Ryan Mitchell <ryan@rt.to> wrote:
Author: Claude Schneegans
>>im looping through a string, replacing emails with an obfuscated/hidden version of the email address. I may have a simpler solution: I have these CF and JS functions in Application.cfm: </CFSCRIPT> function hideAddress(address) { return replace(replace(address, "@", "$"), ".", ";", "all"); } </CFSCRIPT> <SCRIPT> function showAddress(address) { address.href = address.href.replace(/;/g, "."); address.href = address.href.replace(/\$/, "@"); } <CFOUTPUT>userAreas="#session.areas#".split(",");</CFOUTPUT> </SCRIPT> hideAddress replaces @ by $ and the dot by a semicolon in the address. and on client side, showAddress will restore the correct address before opening the user's mailer. eMail sniffer robots won't see the addresses. If ever this is what you are tring to do. -- _______________________________________ REUSE CODE! Use custom tags; See http://www.contentbox.com/claude/customtags/tagstore.cfm (Please send any spam to this address: piegeacon@internetique.com) Thanks.
Author: Claude Schneegans
... I forgot to mention this to put a mailto in your code, use: <A HREF="mailto:#hideAddress(infoEmail)#" onClick="getInfo(this)"> -- _______________________________________ REUSE CODE! Use custom tags; See http://www.contentbox.com/claude/customtags/tagstore.cfm (Please send any spam to this address: piegeacon@internetique.com) Thanks.
Author: Peter Boughton
It will work on the whole string, not just the back reference. Ryan, I don't think it's something that can be done with CF, but it might be possible to use Java's regex instead. Have a look at these two blog entries which might help: http://www.bennadel.com/index.cfm?dax=blog:301.view http://www.bennadel.com/blog/191-REReplace-Java-Function-Pointers-Freakin-Sexy-.htm
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||