how to get value of specifc string in coldfusion with regex - regex

I am trying to get the url value of the following string with coldfusion, i am using list function but i am lost how do i get that please advice
<cfsavecontent variable="foo">
function modalwindow() {
url = "http://www.idea.com?mycode=9&pagenum=-1&sortBy=1&sortOrder=1";
mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
mywin.focus();
return false;
}
</cfsavecontent>
<cfset a = listgetat(foo,2,"url")>
<cfoutput>#a#</cfoutput>
but i am getting weird results, i need to fetch the URL value

This regex will return the URL and accounts for the whitespace being optional, however I should add the disclaimer that this is a little brittle and probably not a good way of going about whatever it is you're after for several reasons.
#reReplaceNoCase( foo, '.*url\s*=\s*"(.*?)".*', '\1' )#

Related

How to extract part of url - dart/flutter

I'm trying to extract the part of url (To be more specific, I'm trying to extract the value of page_info parameter in the url which is next to rel="next"
String testUrl = "<https://demo.myshopify.com/admin/api/2022-01/products.json?limit=10&page_info=eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6NjczMDU4MDcyMTc1NCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBCcmFjZWxldCJ9>; rel='previous', <https://demo.myshopify.com/admin/api/2022-01/products.json?limit=10&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NjczMDIyNzcxMjA5MCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBIZWFydCBQZW5kYW50IE5lY2tsYWNlIn0>; rel='next'";
List<String> splitUrl = testUrl.split("=");
print(splitUrl[5]);
// this is what it prints out
eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NjczMDIyNzcxMjA5MCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBIZWFydCBQZW5kYW50IE5lY2tsYWNlIn0>; rel
// this is what I'm trying to extract
eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NjczMDIyNzcxMjA5MCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBIZWFydCBQZW5kYW50IE5lY2tsYWNlIn0
// value for rel="next"
I tried to split the url by using split function on String but that would also bring the angle bracket with it. I'm trying to extract only page_info= parameter value which is for rel="next"
I know this has to do something with regex but I'm not really good at it! Any help would be really appreciated
I grabbed that url from header response (paginated REST API), it returns two page_info parameters (one for next and other one for previous page) I'm trying to extract value for next page. Splitting the url didn't help me
thank you
An alternative approach is to use Uri.parse to parse the URL:
void main() {
String testUrl = "<https://demo.myshopify.com/admin/api/2022-01/products.json?limit=10&page_info=eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6NjczMDU4MDcyMTc1NCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBCcmFjZWxldCJ9>; rel='previous', <https://demo.myshopify.com/admin/api/2022-01/products.json?limit=10&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NjczMDIyNzcxMjA5MCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBIZWFydCBQZW5kYW50IE5lY2tsYWNlIn0>; rel='next'";
// Extract just the URL.
var match = RegExp(r'<([^>]*)>').firstMatch(testUrl);
if (match != null) {
var uri = Uri.parse(match.group(1)!);
print(uri.queryParameters['page_info']); // Prints: eyJkaXJlY3Rpb24iOiJ...
}
}
Note that the above wouldn't need any of the RegExp code if testUrl were a proper URL without the angle brackets and rel='next' junk.
the regEx pattern page_info=([\w]+)
gives you
eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6NjczMDU4MDcyMTc1NCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBCcmFjZWxldCJ9
eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NjczMDIyNzcxMjA5MCwibGFzdF92YWx1ZSI6IjE4SyBHb2xkIFBsYXRlZCBIZWFydCBQZW5kYW50IE5lY2tsYWNlIn0
https://regexr.com/6qj1h

fixing the url from the string using replace

I am doing a cfhttp call using a cfx_http5 tag as it is faster and better than cfhttp. so the links are coming as:
sort A
so i add the following script using the replace
http://mysubdomain.domain.com/http://mysubdomain.domain.com/e9.asp?rpttype=298&sortBy=1&sortOrder=2
<cfset lnk = ReplaceNoCase(objget, 'href="', 'href="http://mysubdomain.domain.com/', 'all')>
in few of the links, it is coming as correct but in few of the links it is coming as above appending one more to the already existing one,
so i want to make it conditional, if it exists, do not append or add or else add if there is no link
any ideas?
You can use regular expressions with negative lookahead like this:
<cfset lnk = reReplaceNoCase(objget, 'href=\"(?!http\:\/\/)','href="http://mysubdomain.domain.com', 'ALL')>
This will work for both type of links.
Simply create a conditional if statement
lnk = 'http://mysubdomain.domain.com/e9.asp?rpttype=298&sortBy=1&sortOrder=2';
if (!findNoCase('mysubdomain.domain.com', lnk)) {
lnk = ReplaceNoCase(objget, 'href="', 'href="http://mysubdomain.domain.com/', 'all');
}

Find and Replace in ColdFusion

Here is the code which is troubling me. I need to find the code and replace with Fancybox code, but it is throwing an error: win is undefined
replace the mywin value
<cfsavecontent variable="x">
mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=ye‌s,width=878,height=810');
</cfsavecontent>
to this:
mywin = $.fancybox('href' :url,'width': '500');
I am trying like this
<cfset a = Replace(x,"mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>
Update Code:
<cfsavecontent variable="foo">
function setmycode() {
url = "http://myurl.com?thestep=9&sortBy=1&sortOrder=1";
mywin = window.open (url,"win",'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
mywin.focus();
return false;
}
</cfsavecontent>
<cfset a = Replace(foo,"mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>
<cfdump var="#a#">
You need to pay attention to your code. Even the syntax highlighting in your very question actually points out the error to you!
If an error says something about "win" and the text "win" is highlighted by a syntax highlighter... it's a bit of a give away that that is where the error lies. So that's what you should be scrutinising.
You have a string delimited with double quotes, but the string itself has double quotes in it:
mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
So the CF parser sees the first double quote around "win" as the end of the string. And then what follows is just garbage (and will yield a syntax error, as you are seeing).
If your string has string-delimiters within it, you need to escape them. In CFML one does this by doubling them, eg: "" ("Special characters")
So your string becomes:
mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
And when delimited, it is now parseable by CF:
<cfset a = Replace(x,"mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>

How to use a wildcard in isDefined() or structKeyExists() in a URL?

I am using ColdFusion 8.0.1.
I am writing a little code in the application file that will look at the URL. If any of a certain type of property is passed, I don't want to update a property in a SESSION structure.
Basically, if a visitor accesses any page that has to do with our registration process, we do not want to update the SESSION.UserInfo.ReturnToURL variable. For every other page they access, we want to update the variable.
All pages that have to do with the registration process will have "myiq.reg" in the URL. If this were the case, I would use the code below.
// DETERMINE WHETHER TO UPDATE RETURNTOURL
if (not structKeyExists(URL, "myiq.reg")) {
URLString = "http://" & CGI.SERVER_NAME & CGI.SCRIPT_NAME & CGI.QUERY_STRING;
SESSION.UserInfo.ReturnToURL = URLString;
}
But it's not that simple. My people want to be able to pass other properties that are similar, like this:
myiq.reg_confirm
myiq.reg_password
myiq.reg_save
I need to be able to soft code these to work with any registration page that they might create in the future. Basically, I need something like this :
if (not structKeyExists(URL, "myiq.reg*")) {
SESSION.UserInfo.ReturnToURL = URLString;
}
Notice the WILDCARD after "myiq.reg". I've tried this, but it doesn't work.
How do I code this so that any page that is access with a URL property that begins with "myiq.reg" is ignored?
You could get a structKeyList() of the URL scope, and just do a regex find in that. Something like:
reFindNoCase("(?:^|,)myiq\.reg", structKeyList(URL))
(only superficially tested)
You could improve the regex a bit if you wanted to more accurately match actual variable name patterns rather than just any occurrence of myiq.reg in the string.
Something like this perhaps...
res = '';
params = StructKeyList(url);
for(i=1; i lte ListLen(params); i++) {
param = listGetAt(params, i);
if (CompareNoCase(Left(param, 8), 'myiq.reg') eq 0) {
res = param;
break;
}
}

How to match and return part of an HTML file in ASP

I'm looking to match a part of several HTML files that get passed into a loop in an ASP file and then return that part of the HTML files to include in my output. Here's my code so far:
<%for i=0 to uBound(fileIDs) ' fileIDs is an array of URLs
dim srcText, outText, url
Set ex = New RegExp
ex.Global = true
ex.IgnoreCase = true
ex.Pattern = "<section>[\S\s]+</section>" ' This finds the HTML I want
url = fileIDs(i)
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
srcText = xmlhttp.responseText
outputText = ex.Execute(mediaSrcText) ' I expect this to be the HTML I want
Response.Write(outputText.Item(0).Value) ' This would then return the first instance
set xmlhttp = nothing
next %>
I've tested the regular expression on my files and it's matching the parts that I want it to.
when I run the page containing this code, I get an error:
Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method
on the line with ex.Execute. I've also tried ex.Match, but got the same error. So I'm clearly missing the proper method for returning the match so I can write it out into the file. What is that method? Or am I approaching the problem from the wrong direction?
Thanks!
You need a Set when you're assigning outputText:
Set outputText = ex.Execute(mediaSrcText)
I should probably also say that you really shouldn't be using regular expressions to attempt to parse HTML, although I don't know enough about the context to offer more specific advice.