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

3
  • So add an optional space in the reg exp. Aso seems like it is a bug if it has a leading space Commented Oct 17, 2016 at 17:56
  • What about answer here => stackoverflow.com/a/15926317/929902 ? 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. Commented Oct 17, 2016 at 18:01

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, This was a helpful approach and works well without jquery.
1

You may want to use the * quantifier that says "any number of times including zero" combined 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

2 Comments

i think this is what would be most helpful but it does not seem to work.
nvm, I was using var reg = new RegExp(pattern, 'i'); to create the pattern and therefor needed to escape the backslash character, Thank you, different from my title but indeed most helpful answer
1

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];

Comments

0

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])

1 Comment

jQuery is not tagged in the question. And "I'll try to [answer fully later]" isn't useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.