ちょっと必要になったので、
タグや空白を無視して、ある文字列の中から、
指定した文字列がマッチする範囲を計算するJavaScript
を書いてみました。

js>>
/* (C) Genki Takiuchi. New BSD License */
var findSelection = function(html, sel){
var chars = html.split(''), sels = sel.split('');
var start = 0, end = 0, step = 0, tag = false;
var cur = function(){return chars[end]};
var parse = function(){
switch(cur()){
case '>': if(tag) tag = false; return go();
case '<': if(!tag) tag = true; return go();
case '\n': return go();
case ' ': return go();
case '\t': return go();
default: return tag ? in_tag() : out_tag();
}
};
var go = function(){return(chars[end++] ? parse:reject)()};
var in_tag = function(){return go()};
var out_tag = function(){
return(cur() == go_sel() ? match:not_match)()
};
var not_match = function(){step = 0; return go()};
var match = function(){
if(step == 1) start = end;
return(sels[step] ? go:accept)()
};
var accept = function(){return [start, end + 1]}
var reject = function(){return [0, 0]};
var go_sel = function(){
var s = sels[step++];
return white_space(s) ? go_sel() : s
};
var white_space = function(c){
return c && c.match(/[\s\n]/)
};
return parse();
};
<<js

使い方は、こんな感じ。

js>>

findSelection('hello, world!', 'llo, world')
[2, 15]
<<js

選択範囲を取得して、選択している領域をRangeを求めるために作りました。
selに指定した文字列にマッチする箇所が複数ある場合は、
最初にマッチした場所のRangeを返します。

posted by genki genki on Sun 9 Mar 2008 at 10:54 with 0 comments
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://khvanchkaravino.ru https://khvanchkaravino.ru: Квартира мечты за 2 месяца — помогаем выбрать мебе... 05/16 05:06
https://www.tequilagames.com https://www.tequilagames.com: Thanks for finally writing about >something new an... 05/16 03:16
https://%EB%B6%80%EB%B9%84%EC%A3%BC%EC%86%8C.isweb.co.kr/ https://%EB%B6%80%EB%B9%84%EC%A3%BC%EC%86%8C.isweb.co.kr/: We absolutely love your blog and find a lot of you... 05/16 01:40
https://zgarcitul01.com https://zgarcitul01.com: Hi my loved one! I wish to say that this post is a... 05/16 01:10
https://pelnadobrego.pl https://pelnadobrego.pl: I love what you guys tend to be up too. This sort ... 05/15 22:11
https://bet365info.org/%f0%9f%92%b8-%d8%a8%d8%a7-%d9%87%db%8c%da%86%db%8c-%d8%af%d8%b1-%d8%a7%d9%86%d9%81%d8%ac%d8%a7%d8%b1-%d9%85%db%8c%d9%84%db%8c%d9%88%d9%86%d8%b1-%d8%b4%d9%88-%d8%b1%d8%a7%d9%87%d9%86%d9%85%d8%a7%db%8c/ https://bet365info.org/%f0%9f%92%b8-%d8%a8%d8%a7-%d9%87%db%8c%da%86%db%8c-%d8%af%d8%b1-%d8%a7%d9%86%d9%81%d8%ac%d8%a7%d8%b1-%d9%85%db%8c%d9%84%db%8c%d9%88%d9%86%d8%b1-%d8%b4%d9%88-%d8%b1%d8%a7%d9%87%d9%86%d9%85%d8%a7%db%8c/: درود، بنده امروز اتفاقی در اینترنت با این وبسایت آ... 05/15 21:26
https://linklist.bio/jutawanbetlogin https://linklist.bio/jutawanbetlogin: Hi there, You've done an excellent job. I will ce... 05/15 21:20
https://tarkovescapezone.com https://tarkovescapezone.com: Today, I went to the beachfront with my children. ... 05/15 20:33