javascript - 在数字/数字和字母/字符之间添加空格

标签 javascript split space digits letters

我有这样的代码

(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”并且脚本无法将输入与类似于“ip 1500”的元素匹配。我的问题是我是 js 初学者。

我一直在尝试,但我无法让它工作。我也试过this

我找到了这个地方,我认为它可以在这里完成,所有的东西都被一个“”(空格)分开:

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

如果有人能帮助我,那就太好了。

最佳答案

如果你想要“123abc345def”到“123 abc 345 def”。替换功能可能会有所帮助。代码是这样的。

var str = "123abc345def";
str = str.replace(/(\d+)/g, function (_, num){
    console.log(num);
    return ' ' + num + ' ';
});
str = str.trim();

关于javascript - 在数字/数字和字母/字符之间添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13191714/

相关文章:

java - 是否可以在 java 中拆分 "."附近的字符串?

Vbscript 修剪函数

c - 如何将空格分隔的 float 从字符串中提取到 c 中的另一个数组中?

javascript - 如何实现交互式用户指南?

javascript - 将 javascript 变量传递给外部 php 文件

javascript - 在上传到 AWS S3 时计算 Node.js 中本地文件的 ETag

java - 如何将字符串与整数分开并保存

c# - 用完整的字符串分隔符拆分字符串

java - Struts 应用程序的 html 中的额外空间从何而来?

javascript - 在鼠标悬停时将 Div 标签滑出