最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - How to get href of string in Javascript - Stack Overflow

programmeradmin6浏览0评论

a 3rd party tool I'm using builds an anchor tag like so..

"<a href="http://DevNode/Lists/Publications.aspx#/publication/123">http://DevNode/Lists/Publications.aspx#/publication/123</a>"

I need to isolate the href so I can trim it. Currently my pattern of

reg = /^(<a\shref=")? http:\/\/DevNode\/Lists\/Publications.aspx#\/publication\/(\d+)/i {lastIndex: 0} 

will fail to match if the href has a leading space like this

"<a href=" http://DevNode/Lists/Publications.aspx#/publication/123"> http://DevNode/Lists/Publications.aspx#/publication/123</a>"

Please help

a 3rd party tool I'm using builds an anchor tag like so..

"<a href="http://DevNode/Lists/Publications.aspx#/publication/123">http://DevNode/Lists/Publications.aspx#/publication/123</a>"

I need to isolate the href so I can trim it. Currently my pattern of

reg = /^(<a\shref=")? http:\/\/DevNode\/Lists\/Publications.aspx#\/publication\/(\d+)/i {lastIndex: 0} 

will fail to match if the href has a leading space like this

"<a href=" http://DevNode/Lists/Publications.aspx#/publication/123"> http://DevNode/Lists/Publications.aspx#/publication/123</a>"

Please help

Share Improve this question asked Oct 17, 2016 at 17:53 Jeremy NelsonJeremy Nelson 2861 gold badge8 silver badges19 bronze badges 3
  • So add an optional space in the reg exp. Aso seems like it is a bug if it has a leading space – epascarello Commented Oct 17, 2016 at 17:56
  • What about answer here => stackoverflow./a/15926317/929902 ? – Teoman shipahi Commented Oct 17, 2016 at 17:57
  • @epascarello yes I agree, I've reported it to the 3rd party. How do I add an exemption for infinite optional spaces, I'm not familiar with regex. – Jeremy Nelson Commented Oct 17, 2016 at 18:01
Add a ment  | 

4 Answers 4

Reset to default 7

If you're doing this on a browser the simplest way is to let the browser figure it out:

var div = document.createElement("div");
div.innerHTML = yourString;
var href = div.querySelector("a").href;

This also has the advantage of resolving it if it's a relative URL. If you don't want that, use getAttribute instead:

var href = div.querySelector("a").getAttribute("href");

Note that if you use getAttribute, if the attribute has a leading space, the leading space will be in the result; String#trim could be useful if you want to get rid of it.

You may want to use the * quantifier that says "any number of times including zero" bined to the \s it will match spaces, newlines or else.

So use \s+ where a space is required but there might be more than one

And use \s* where a space is optional but there might be some

reg = /^(<a\s+href=")?\s*http:\/\/DevNode\/Lists\/Publications.aspx#\/publication\/(\d+)/i

Keep it simple:

var ahref = '<a href="http://DevNode/Lists/Publications.aspx#/publication/123">http://DevNode/Lists/Publications.aspx#/publication/123</a>';
var href = ahref.split('"')[1];

An easy/fast answer is using jQuery, building the tag, and looking for the href attribute.

$('<a href="http://DevNode/Lists/Publications.aspx#/publication/123">http://DevNode/Lists/Publications.aspx#/publication/123</a>')
.attr('href')

I'll try to get you the RegExp in a bit. Hang on tight...

And as promissed... here's the RegExp

var text = '<a href="http://DevNode/Lists/Publications.aspx#/publication/123">http://DevNode/Lists/Publications.aspx#/publication/123</a>';
console.log(text.match(/<a\s+(?:[^>]*?\s+)?href="([^"]*)"/)[1])

发布评论

评论列表(0)

  1. 暂无评论