Extract value out of another value (possibly using RegEx)
What I am looking for is a way to extract the URL out of an href attribute. I care about all parameters EXCEPT the sessionToken parameters.
So this…
<A class=ContentURL href="javascript:void(window.open('../content/ModuleList.aspx?PageID=module&sessionToken=[sessionToken]','report','scrollbars=y开发者_JS百科es,resizable=yes,width=640,height=500'))">Manage Applications</A>
Should give me this…
../content/ModuleList.aspx?PageID=module
And this…
<A class=ContentURL href="javascript:void(window.open('../content/ModuleList.aspx?PageID=module&sessionToken=[sessionToken]&Token=DateTime','report','scrollbars=yes,resizable=yes,width=640,height=500'))">Manage Applications</A>
Should give me this…
../content/ModuleList.aspx?PageID=module&Token=DateTime
I have to imagine there is a way to do this using RegEx and I was hoping someone could show me how.
If all of your links have the same format you specified, then pattern for extracting URLs is simple:
href="javascript:void\(window\.open\('(.+?)'.*?"
URL will be captured by the first group: (.+?)
.
Now having extracted URLs, you can do anything you want with them. If you want to remove sessionToken parameter without breaking GET string (by accidently removing ?
or &
character), you should replace (\??)sessionToken=[^&]*&?
with 1st capturing group (a ?
if sessionToken is a first parameter and an empty string if it's not). You might also want to remove trailing &
if sessionToken is a last parameter, but that's not necessarily.
精彩评论