Coldfusion Query Anchor Element by Id - coldfusion

I am running a query and am try to output the information using cfoutput like this:
<cfoutput query="the_query">
<p>#QueryResult#<p>
</cfoutput>
Coldfusion won't allow me to uses the # in href. It says "Invalid CFML construct", but I need it to be href="#". Is there a way to escape this?

Just double up on the # character. ## inside a tag will output a single #.
<cfoutput query="the_query">
<p>#QueryResult#<p>
</cfoutput>
No problem putting these up against regular terms, either, say you wanted to name the anchor using a field from the query:
<p>#QueryResult#<p>
This would give you
<p>Result Here<p>

Related

Removing Code by using rereplace

Hi Have the following code, I am using the following code to remove the contents from the page which i do not know:
I am using regex, and i cannot use jsoup, please do not provide any jsoup link or code because that will be useless to use here for me..
<cfset removetitle = rereplacenocase(cfhttp.filecontent, '<title[^>]*>(.+)</title>', "\1")>
Now above the same way, i want to use the follwoing things:
1. <base href="http://search.google.com">
2. <link rel="stylesheet" href="mystyle.css">
3. and there are 5 tables inside the body, i want to remove the 2nd table.,
Can anyone guide on this
Scott is right, and Leigh was right before, when you asked a similar question, jSoup is your best option.
As to a regex solution. This is possible with regex but there are problems that regex cannot always solve. For instance, if the first or second table contains a nested table, this regex would trip. (Note that text is not required between the tables, I'm just demonstrating that things can be between the tables)
(If there is always a nested table, regex can handle it, but if there is sometimes a nested table, in other words: unknown), it gets a lot messier.)
<cfsavecontent variable="sampledata">
<body>
<table cellpadding="4"></table>stuff
is <table border="5" cellspacing="7"></table>between
<table border="3"></table>the
<table border="2"></table>tables
<table></table>
</body>
</cfsavecontent>
<cfset sampledata = rereplace(sampledata,"(?s)(.*?<table.*?>.*?<\/table>.*?)(<table.*?>.*?<\/table>)(.*)","\1\3","ALL") />
<cfoutput><pre>#htmleditformat(sampledata)#</pre></cfoutput>
What this does is
(?s) sets . to match newlines as well.
(.*?<table.*?>.*?<\/table>.*?) Matches everything before the first table, the first table, and everything between it and the second table and sets it as capture group 1.
(<table.*?>.*?<\/table>) Matches the second table and creates capture group 2.
(.*) matches everything after the second table and creates capture group 3.
And then the third paramters \1\3 picks up the first and third capture groups.
If you have control of the source document, you can create html comments like
<!-- table1 -->
<table>...</table>
<!-- /table1 -->
And then use that in the regex and end up with a more regex-friendly document.
However, still, Scott said it best, not using the proper tool for the task is:
That is like telling a carpenter, build me a house, but don't use a hammer.
These tools are created because programmers frequently run into precisely the problem you're having, and so they create a tool, and often freely share it, because it does the job much better.

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 string of everything between these two em tag?

I want to get string between em tag , including other html also.
for example:
<em>UNIVERSALPOSTAL UNION - International Bureau Circular<br />
By: K.J.S. McKeown</em>
output should be as:
UNIVERSALPOSTAL UNION - International Bureau Circular<br />
By: K.J.S. McKeown
please help me.
Thanks
Use the regular expression function like this:
REMatch("(?s)<em>.*?</em>", html)
See also: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=regexp_01.html
The (?s) sets the mode to single line, so that the input text is interpreted as one line even if it contains line feeds. This is probably the default (I'm not sure) so it can be omitted. As Peter pointed out in a comment, this is not the default and therefore must be set.
The .*? matches all characters inbetween <em> and </em>. The questionmark after the multiplier makes it "non-greedy", so that as few as possible characters are matched. This is needed in case the input html contains something like <em>foo</em><em>bar</em> where otherwise only the outermost <em></em> tags are considered.
The returned array contains all matches found, i.e. all texts including html that was in <em> tags.
Note that this could fail for circumstances where </em> also occurs as attribute text and is incorrectly not html-encoded, for example: <em><a title="Help for </em> tag">click</a></em> or in other rare circumstances (e.g. javascript script tags etc.). A regex cannot replace a full HTML/XML parser and if you need 100% accurateness, you should consider using one: http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_t-z_23.html
If your input is exactly in the format given above, you don't even need regex - just strip the outer tags:
<cfsavecontent variable="Input">[text from above]</cfsavecontent>
<cfset Output = mid( Input, 4 , len(Input) - 9 />
If your input is more than this (i.e. a significant piece of HTML, or a full HTML document), regex is still not the ideal tool - instead, you should be using a HTML parser, such as JSoup:
<cfset jsoup = createObject('java','org.jsoup.Jsoup') />
<cfset Output = jsoup.parse(Input).select('em').html() />
(With CF8, this code requires placing the jsoup JAR file in CF's lib directory, or using a tool such as JavaLoader.)
If you are using jquery you can do this also pretty easily.
$("em").html();
Will return all html between the em tags.
See this fiddle
I had to remove any text that was to follow after a partiucular tag . Now the HTML content was getting generated dynamically from a database that cater to 5 different langauges. so I only had the div tag to help me. I am not sure why REMatch("(?s).*?", html) did not work for me. However Ben helped me here (http://www.bennadel.com/blog/769-Learning-ColdFusion-8-REMatch-For-Regular-Expression-Matching.htm). My code looks like tghis:
<cfset extContentArr = REMatch("(?i)<div class=""inlineBlock"" style=""margin-right:30px;"">.+?</div>",qry_getContent.colval) />
<cfif !ArrayIsEmpty(extContentArr)>
Loop the array and do whatever you need with the extract , I just deleted them.
</cfif>

ColdFusion - Regex to match SRC with single quotes

I have a simple regex line to extract the src="" value from an image tag:
<cfset variables.attrSrc = REMatch("(?i)src\s*=\s*""[^""]+", variables.myImageTag) />
<!--- REMatch("(?i)src\s*=\s*""[^""]+" --->
However, while this works great, it doesn't appear to be working with src='' attrubutes that display single quotes instead of double.
Ideally, I'd like it to work with both single quotes and double.
Any thoughts?
Thanks,
Michael.
(?i)src\s*=\s*(""[^""]+""|'[^']+')

How I create a regular expression to find lines of code that contain cflocation but do not contain addtoken?

I need a regular expression that will find lines with:
<cflocation url="index.cfm" addtoken="No">
but without the addtoken
<cflocation url="index.cfm">
The index.cfm can be any web address
I also added a comment below but this is for my text editor so I can search in files for all cflocation tags that are missing addtoken.
Thanks!
Try <cflocation\s+url="[^"]*"\s*>
You can test out regex's with data at regexpal.com.
You can use strfriend.com to explain regex's, an example output for the regex above is given below: