简体   繁体   English

将输入字符串匹配到文本/元素并突出显示反应式

[英]Match String of Input to text/element and highlight reactive

HTML(JADE) HTML(JADE)

p#result Lorem ipsum is javascript j s lo 1 2 4 this meteor thismeteor. meteor
input.search

JS JS

Template.pg.events({
    'keyup .search': function(e){
        e.preventDefault();

        var text = $('p#result').text();
        var splitText = text.match(/\S+\s*/g);
        var input = $(e.target).val();
        var splitInput = input.match(/\S+\s*/g);

        if(_.intersection(splitText, splitInput)) {
            var match = _.intersection(splitText, splitInput);
            var matchToString = match.toString();
        $('p#result').text().replace(matchToString, '<b>'+matchToString+ '</b>')
        }

        console.log(splitText); //check what I get
        console.log(splitInput); //check what I get

    }
})

I have the above code. 我有上面的代码。

What I'm trying to do is matching the input field's value, and then matching the text. 我想做的是匹配输入字段的值,然后匹配文本。 I added it the function to keyup so that this is reactive. 我将其功能添加到keyup从而可以进行响应。

When the fields and text match, it will add bold tags to the matched strings (texts). 当字段和文本匹配时,它将在匹配的字符串(文本)中添加bold tags

I think I'm almost there, but not quite yet. 我想我快到了,但还没到。

How would I proceed on from here? 我将如何从这里继续?

MeteorPad Here 陨石 在这里

In your code, you seem to only be matching on whole words, although your question does not specify that. 在您的代码中,您似乎只对整个单词进行匹配,尽管您的问题并未对此进行说明。 If you want to match on any text in the input (eg, if you type "a", all "a"s in the text are made bold), you can do that relatively easily using the javascript split and join String methods: 如果要匹配输入中的任何文本(例如,如果键入“ a”,则文本中的所有“ a”均以粗体显示),则可以使用javascript splitjoin String方法相对容易地做到这一点:

Template.pg.events({
    'keyup .search': function(e){
        e.preventDefault();

        var text = $('p#result').text();
        var input = $(e.target).val();
        var splitText = text.split(input); // Produces an array without whatever's in the input
        console.log(splitText);
        var rep = splitText.join("<b>" + input + "</b>"); // Produces a string with inputs replaced by boldened inputs
        console.log(rep);
        $('p#result').html(rep);
    }
});

Notably, you have to replace the text on the page using $('p#result').html() , which was missing in your MeteorPad example. 值得注意的是,您必须使用MeteorPad示例中缺少的$('p#result').html()替换页面上的文本。 Note also that this is a case-sensitive implementation; 还要注意,这是区分大小写的实现; you can use a regex to do the split , but it gets a bit more complicated when you want to replace the text in the join . 你可以使用正则表达式做split ,但是当你要替换的文字就变得更复杂一些join You can play around with it on this MeteorPad . 您可以在此MeteorPad上玩它。

To do this case-insensitively, the split is very straightforward -- you can use a RegExp like so: 要不区分大小写地执行此操作,拆分非常简单-您可以像这样使用RegExp:

...
var regex = new RegExp($(e.target).val(), 'gi'); // global and case-insensitive, where `input` used to be

The tricky thing is to extract the correct case of what you want to pull out, then put it back in -- you can't do this with a simple join , so you'll have to interleave the two arrays. 棘手的事情是提取要提取的内容的正确大小写,然后再放回去-您不能通过简单的join来做到这一点,因此您必须交错两个数组。 You can see an example of interleaved arrays here , which was taken from this question . 您可以在此处看到交错数组的示例,该示例取自此问题 I've amended that a bit to deal with the uneven array lengths, here: 我对此进行了一些修改,以处理不均匀的数组长度,在这里:

var interleave = function(array1, array2) { 
  return $.map(array1, function(v, i) { 
    if (array2[i]) { return [v, array2[i]]; } // deals with uneven array lengths
    else { return [v]; }
  });
}

I've also created another MeteorPad that you can play around with that does all of this. 我还创建了另一个MeteorPad ,您可以使用它完成所有这些工作。 lo is a good test string to check out. lo是签出的好测试字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM