Try the following:
/<a[^>]*>([\s\S]*?)<\/a>/
<a[^>]*> matches the open tag a([\s\S]*?) matches any characters before the closing tag as little as possible<\/a> matches the closing tag
([\s\S]*?) captures the text between the tags as argument 1 in the array returned by calling exec or match .
This is really useful for finding text inside a elements, it is not incredibly safe or reliable, but if you have a large page of links and you just need their text, this will do it.
A safer way to do this without RegExp:
function getAnchorTexts(htmlStr) { var div, anchors, i, texts; div = document.createElement('div'); div.innerHTML = htmlStr; anchors = div.getElementsByTagName('a'); texts = []; for (i = 0; i < anchors.length; i += 1) { texts.push(anchors[i].text); } return texts; }
zzzzBov
source share