Reputation: 469
I am getting raw html from one site in javascript.
Here is the example part:
<a href="/?Stat=5&item=10739">Coldwater</a></b>
Now I use exec
to pull some data out with pattern:
Stat=5&item=(\d.*)">(.*)<\/a><\/b>
It works fine in regex tester (link), problem is how to write in js, currently I have this code (returns null):
$.get(link,function(data) {
var raw = data,
pattern = / Stat=5&item=(\d.*)">(.*)<\/a><\/b>/gi,
matches = pattern.exec(raw);
console.log(matches);
});
Probably I have to remove some single/double quotes, slashes from that raw html?
Upvotes: 1
Views: 312
Reputation: 41842
Here there is no need to use regex. You can achieve the same by creating a new element.
var a = document.createElement('div');
a.innerHTML = yourString;
var result = a.children[0].href;
Upvotes: 7
Reputation: 174766
Remove the space before the string Stat
,
> var str = '<a href="/?Stat=5&item=10739">Coldwater</a></b>';
undefined
> console.log(/Stat=5&item=(\d.*)">(.*)<\/a><\/b>/gi.exec(str)[0]);
Stat=5&item=10739">Coldwater</a></b>
> console.log(/Stat=5&item=(\d.*)">(.*)<\/a><\/b>/gi.exec(str)[1]);
10739
> console.log(/Stat=5&item=(\d.*)">(.*)<\/a><\/b>/gi.exec(str)[2]);
Coldwater
Upvotes: 1