バックリファレンスの使い方
20日に書いたやつの続き。バックリファレンスはこんな風に使う。以下の見本は昨日「はまちちゃん」で見つけた某ブックマークレットのソース。ブックマークレットの宿命として原本では文字数をぎりぎりまで削ってあるから、見やすいように適宜改行とかインデントとか余分なスペースとか入れた。
javascript:(function(){ l=document.getElementsByTagName('a'); n=new Array('%E3%82%B3%E3%83%9F','%E7%B4%B9','%E5%8F%8B','%E5%86%99','%E3%82%B3%E3%83%A1','%E6%97%A5'); a=new Array('list_community','show_intro','list_friend','show_photo','list_comment','list_diary'); for(i=0;i<l.length;++i){ if(l[i].href.match(/show_friend.pl.id=(\d+)/)){ for(j=0;j<n.length;++j){ c=document.createElement('a'); c.setAttribute('href',a[j]+'.pl?id='+RegExp.$1); c.appendChild(document.createTextNode('['+n[j]+']')); l[i].parentNode.insertBefore(c,l[i].nextSibling); } } } })()
RegExp.$1
というところ。その3行前のmatch()
内の(\d+)
でキャプチャしたやつを こうやって参照している