Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

REReplaceNoCase Find and append HREF Strings

Community Beginner ,
Feb 25, 2011 Feb 25, 2011

I have an index.html file which I am reading via cffile.

I want to search all of the href string and append with a custom string. For example, the file will read a bunch and a href links ( <a href="http://www.google.com">Google</a>,<a href="http://www.yahoo.com">Yahoo</a> etc). I want to be able to append all of the href with something like this (<a href="http://www.mysite.com?redirect=http://www.google.com">Google</a>. I would really appreciate your help in advanced. Thank you.

587
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Feb 25, 2011 Feb 25, 2011

Something like this should work at a basic level, assuming the HTML is valid:

<cfset newHTML = ReReplaceNoCase(html, "(href=[""'])([^""']+)([""'])", "\1http://mysite.com/?redir=\2\3", "ALL")>

It's using backreferences in the regex the parenthesis to group the parts of the match, which are then referred to as \1 \2 and \3 in the replacement string.

Translate
Enthusiast ,
Feb 25, 2011 Feb 25, 2011
LATEST

Something like this should work at a basic level, assuming the HTML is valid:

<cfset newHTML = ReReplaceNoCase(html, "(href=[""'])([^""']+)([""'])", "\1http://mysite.com/?redir=\2\3", "ALL")>

It's using backreferences in the regex the parenthesis to group the parts of the match, which are then referred to as \1 \2 and \3 in the replacement string.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources