ちょっと必要になったので、
タグや空白を無視して、ある文字列の中から、
指定した文字列がマッチする範囲を計算する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
http://ldm.sakura.ne.jp/balu+bbbs/album/album.cgi?mode=detail&no=210 http://ldm.sakura.ne.jp/balu+bbbs/album/album.cgi?mode=detail&no=210: Aw, this was an incredibly good post. Taking the t... 04/25 18:19
https://www.bigbeargum.com/2019/02/15/ambrose-redmoon/ https://www.bigbeargum.com/2019/02/15/ambrose-redmoon/: Hello would you mind letting me know which hosting... 04/25 17:39
http://zeroken.jp/1978td/album/album.cgi?mode=detail&no=20 http://zeroken.jp/1978td/album/album.cgi?mode=detail&no=20: Asking questions are genuinely fastidious thing if... 04/25 17:26
https://natucci-estetica.com/?attachment_id=1376 https://natucci-estetica.com/?attachment_id=1376: Hello there, You have done a fantastic job. I wi... 04/25 17:05
https://www.waimaoz.com/23019.html?unapproved=5151&moderation-hash=a14b2a811444fc3059edb7c1441d076e https://www.waimaoz.com/23019.html?unapproved=5151&moderation-hash=a14b2a811444fc3059edb7c1441d076e: Good day! Do you use Twitter? I'd like to follow y... 04/25 16:50
https://bysystem.ru/process-proizvodstva-kartonnyh-korobok-i/ https://bysystem.ru/process-proizvodstva-kartonnyh-korobok-i/: Требовалось производство картонных коробок в сжа... 04/25 15:33
https://marialionsa.ru/proizvodstvo-kartonnyh-korobok-i-upakovki/ https://marialionsa.ru/proizvodstvo-kartonnyh-korobok-i-upakovki/: Документооборот четкий, все закрывающие акты предо... 04/25 15:05
https://example.com/oqaphq https://example.com/oqaphq: This post is genuinely a pleasant one it assists n... 04/25 12:43