正規表現でアンカーテキストを取り出す

TextAreaに入力されているアンカーテキスト形式に誤りがないかチェックするため、まずはすべてのアンカーテキストを配列に取り出すことに。
これが意外と難しかった。

function extractAnchors(text) {
	text = text.replace(/&lt;/g, "<");
	text = text.replace(/&gt;/g, ">");
	var anchors = text.split(/<\/a>/);
	anchors.length--;
	for (var i =0; i < anchors.length; i++) {
		anchors[i] += "</a>";
		anchors[i] = (anchors[i].match(/<a[^>]*>(.||\n)*<\/a>/))[0];
	}
	return anchors;
}


imgタグを取り出す関数もつくってみた。

function extractImgs(text) {
	text = text.replace(/&lt;/g, "<");
	text = text.replace(/&gt;/g, ">");
	return text .match(/<img[^>]*>/g);
}

ふぅ。