2

私はこのようなコードを持っています

(function($, window, document, undefined) {
    $.fn.quicksearch = function (target, opt) {

        var timeout, cache, rowcache, jq_results, val = '', e = this, options = $.extend({ 
            delay: 100,
            selector: null,
            stripeRows: null,
            loader: null,
            noResults: '',
            bind: 'keyup',
            onBefore: function () { 
                return;
            },
            onAfter: function () { 
                return;
            },
            show: function () {
                this.style.display = "";
            },
            hide: function () {
                this.style.display = "none";
            },
            prepareQuery: function (val) {
                return val.toLowerCase().split(' ');
            },
            testQuery: function (query, txt, _row) {
                for (var i = 0; i < query.length; i += 1) {
                    if (txt.indexOf(query[i]) === -1) {
                        return false;
                    }
                }
                return true;
            }
        }, opt);

        this.go = function () {

            var i = 0, 
            noresults = true, 
            query = options.prepareQuery(val),
            val_empty = (val.replace(' ', '').length === 0);

            for (var i = 0, len = rowcache.length; i < len; i++) {
                if (val_empty || options.testQuery(query, cache[i], rowcache[i])) {
                    options.show.apply(rowcache[i]);
                    noresults = false;
                } else {
                    options.hide.apply(rowcache[i]);
                }
            }

            if (noresults) {
                this.results(false);
            } else {
                this.results(true);
                this.stripe();
            }

            this.loader(false);
            options.onAfter();

            return this;
        };

        this.stripe = function () {

            if (typeof options.stripeRows === "object" && options.stripeRows !== null)
            {
                var joined = options.stripeRows.join(' ');
                var stripeRows_length = options.stripeRows.length;

                jq_results.not(':hidden').each(function (i) {
                    $(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]);
                });
            }

            return this;
        };

        this.strip_html = function (input) {
            var output = input.replace(new RegExp('<[^<]+\>', 'g'), "");
            output = $.trim(output.toLowerCase());
            return output;
        };

        this.results = function (bool) {
            if (typeof options.noResults === "string" && options.noResults !== "") {
                if (bool) {
                    $(options.noResults).hide();
                } else {
                    $(options.noResults).show();
                }
            }
            return this;
        };

        this.loader = function (bool) {
            if (typeof options.loader === "string" && options.loader !== "") {
                 (bool) ? $(options.loader).show() : $(options.loader).hide();
            }
            return this;
        };

        this.cache = function () {

            jq_results = $(target);

            if (typeof options.noResults === "string" && options.noResults !== "") {
                jq_results = jq_results.not(options.noResults);
            }

            var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults);
            cache = t.map(function () {
                return e.strip_html(this.innerHTML);
            });

            rowcache = jq_results.map(function () {
                return this;
            });

            return this.go();
        };

        this.trigger = function () {
            this.loader(true);
            options.onBefore();

            window.clearTimeout(timeout);
            timeout = window.setTimeout(function () {
                e.go();
            }, options.delay);

            return this;
        };

        this.cache();
        this.results(true);
        this.stripe();
        this.loader(false);

        return this.each(function () {
            $(this).bind(options.bind, function () {
                val = $(this).val();
                e.trigger();
            });
        });

    };

}(jQuery, this, document));

数字と文字の間にスペースを分割/追加する場所と方法を理解しようとしています。たとえば「ip1500」と入力すると、スクリプトが「ip1500」のような要素と入力を一致させることができない場合があります。私の問題は、私がjsの初心者であるということです。

私は試してみましたが、うまくいきません。私もこれを試しました

私はこの場所を見つけました、そして私はそれがすべてが ""(スペース)によって分割されるここでできると思います:

prepareQuery: function (val) {
    return val.toLowerCase().split(' ');
    }, 

誰かが私を助けてくれるならとてもいいでしょう。

4

2 に答える 2

6

「123abc345def」から「123abc345def」が必要な場合。置換機能が役立つ場合があります。コードはこんな感じです。

var str = "123abc345def";
str = str.replace(/(\d+)/g, function (_, num){
    console.log(num);
    return ' ' + num + ' ';
});
str = str.trim();
于 2012-11-02T10:07:21.780 に答える
3

リンクしたコードは、主にjavascriptとは異なるプログラミング言語を使用しているために機能しませんでした。理論的には機能するはずですが、javascriptは正規表現の後読みをサポートしていません(現時点では)。

代わりに、私はそのコードの断片を書き直しました:

prepareQuery: function (val) {
function isNotLetter(a){
return (/[0-9-_ ]/.test(a));
}

var val=val.toLowerCase().split("");
var tempArray=val.join("").split("");
var currentIndex=1;

for (var i=0;i<val.length-1;i++){
if (isNotLetter(val[i]) !== isNotLetter(val[i+1])){
tempArray.splice(i+currentIndex, 0, " ");
currentIndex++;
}
}
return tempArray.join("");
}

あなたはjavascriptに慣れていないので、それが何をするのかを説明します。

  1. prepareQuery文字列に文字が含まれているかどうかをチェックする関数を宣言します[これは別の場所に移動できます]
  2. val次に、配列に分割し、のコンテンツをにコピーvalしますtempArray
  3. インデックスが宣言されます(後で説明します)
  4. ループが作成され、ループが作成されます。val
  5. このステートメントは、現在の文字(ループによって設定されたもの)がその隣の文字()と同じであるifかどうかを検出します。val[i]val[i+1]
  6. どちらかが他方と異なる場合(つまり、現在の文字が文字であり、次の文字がそうでない場合)、tempArrayその「インデックス」のにスペースが追加されます。
  7. インデックスはインクリメントされ、#6のオフセットとして使用されます
  8. ループが終了し、「配列」を文字列に結合して、結果を出力します。

デモ: http ://jsbin.com/ebitus/1/edit(JSFiddleがダウンしていました...)

編集: 申し訳ありませんが、私はあなたの質問を完全に誤解しました...あなたは「クイックサーチ」とjQueryを使用しているとは言いませんでし。その場合、名前のある要素のリストがあり、プラグインを使用してそれらを検索したいと想定しています...ユーザーのクエリに一致するはるかに簡単な方法(スペースがない場合)は、検索テーブルからのスペースとクエリ自体-元の逆の方法は機能しますが(それほど効率的ではありません)[別名:ユーザーのクエリを拡張する]

この場合、検索テーブルとユーザー入力の両方からスペースを削除する方が適切な方法です。

    prepareQuery: function (val) {
        return val.toLowerCase().replace(/ /ig,'').split(" ");
    },
    testQuery: function (query, txt, _row) {
        txt=txt.toLowerCase().replace(/ /ig,'');
    for (var i = 0; i < query.length; i += 1) {
        if (txt.indexOf(query[i]) === -1) {
            return false;
        }
    }
    return true;

}

デモ:http: //jsfiddle.net/q9k9Y/3/

編集2:

あなたの本当の意図は、文字と数字の間にスペースを追加するだけでなく、Webサイトで完全に機能する検索機能を作成することであるように思われます。これで、 Quicksilverを使用することをお勧めします。quickSearcherを拡張するアルゴリズムを作成したいのですが、現時点ではできません(タイムゾーン)。代わりに、Quicksilverを使用することをお勧めします

http://jsbin.com/oruhet/12/

于 2012-11-02T09:33:06.883 に答える