开发者

What regex can I use to extract the URL from an HTTP <a> element?

Here is the Original string :

var str = "<a href=\"https://sjobs.brassring.com/1033/ASP/TG/cim_home.asp?partnerid=25172&amp;siteid=5235&amp;LanguageId=1/javascript:window.location='cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&amp;jobId=385594&amp;type=hotjobs&amp;JobReqLang=141&amp;JobSiteId=5239&amp;JobSiteInfo=385594_5239&amp;GQId=0'\"> a vartiable</a>";

and I need this part:

str = "https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&开发者_如何学Pythonamp;amp;jobId=385594&amp;type=hotjobs&amp;JobReqLang=141&amp;JobSiteId=5239&amp;JobSiteInfo=385594_5239&amp;GQId=0";

In other words, I need to remove the tag <a> and the document.href value

Thanks guys.


How about:

var str = "<a href=\"https://sjobs.brassring.com/1033/ASP/TG/cim_home.asp?partnerid=25172&amp;siteid=5235&amp;LanguageId=1/javascript:window.location='cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&amp;jobId=385594&amp;type=hotjobs&amp;JobReqLang=141&amp;JobSiteId=5239&amp;JobSiteInfo=385594_5239&amp;GQId=0'\"> a vartiable</a>";
str.replace(/^<a href="(https.*?)cim_home\.asp.*?'(cim_jobdetail\.asp.*)'.*$/, "$1$2");

produces:

"https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&amp;jobId=385594&amp;type=hotjobs&amp;JobReqLang=141&amp;JobSiteId=5239&amp;JobSiteInfo=385594_5239&amp;GQId=0"


Something simple like the following should work...

href="(.*?)"


here's the code u want:

var str = '<a href="https://sjobs.brassring.com/1033/ASP/TG/cim_home.asp?partnerid=25172&amp;siteid=5235&amp;LanguageId=1/javascript:window.location='cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&amp;jobId=385594&amp;type=hotjobs&amp;JobReqLang=141&amp;JobSiteId=5239&amp;JobSiteInfo=385594_5239&amp;GQId=0'"> a vartiable</a>'

var url = /\"(.*?)\"/str

that's how you match, here's how you strip it out:

str.replace(/\"(.*?)\"/, "$1");

the \"(.*?)\" gives the first minimal set of characters between two " characters the id of $1 then the second argument to the replace function tells it to replace the whole string with what's contained in $1

Also, if you use jQuery, this becomes pretty trivial:

var url = $("a").attr("href");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜