正規表現でアンカーテキストを取り出す
TextAreaに入力されているアンカーテキスト形式に誤りがないかチェックするため、まずはすべてのアンカーテキストを配列に取り出すことに。
これが意外と難しかった。
function extractAnchors(text) { text = text.replace(/</g, "<"); text = text.replace(/>/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(/</g, "<"); text = text.replace(/>/g, ">"); return text .match(/<img[^>]*>/g); }
ふぅ。