0

Here's my pattern

<a href="(.*?)" onmousedown="test\(\)">

Here's the source

<tags><tags><a href="http://google.com" onmousedown="test()"></tags></tags>

If I use that pattern, I get this result:

<a href="http://google.com" onmousedown="test()">

What I want to get is just

http://google.com

Any help would be appreciated. Thank you! :)

user1732887
  • 288
  • 1
  • 2
  • 9

1 Answers1

0

I have modified the regex slightly(adding .* at both ends) so that it fits with both the below examples.

var regex = /.*?<a href="([^">]*)" onmousedown="test\(\)">.*/; 
var input = '<tags><tags><a href="http://google.com" onmousedown="test()"></tags></tags>';

Approach#1: Using regex replace. In this case this is matching the whole string with the pattern and replace it with only the first captured group($1).

url1 = input.replace(regex, "$1");
alert(url1)

Approach#2: Using regex matching with the input string and the pattern and after that only picking the first captured group([1]).

url2 = regex.exec(input)[1];
alert(url2);

In both cases the output will be the url from href tag.

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85