Search code examples
javascriptregexlistjs

How to perform Regex on listjs hackerlist?


I would like to perform regex on a search functionality of List.js. How it works by default is that it searches for a word which exists in a whole sentence. What I want to achieve is that it should search a word which is the start of a sentence.

enter image description here

In above picture, you see "Was" exists in all 4 sentences but I only want it to short list where "Was" is the first word of each sentence.

ListJs Source


Solution

  • Thanks guys for suggestions. For now I have done a workaround by modifying the core list.js file. I replaced:

    var search = {
        list: function() {
            for (var k = 0, kl = list.items.length; k < kl; k++) {
                search.item(list.items[k]);
            }
        },
        item: function(item) {
            item.found = false;
            for (var j = 0, jl = columns.length; j < jl; j++) {
                if (search.values(item.values(), columns[j])) {
                    item.found = true;
                    return;
                }
            }
        },
        values: function(values, column) {
            if (values.hasOwnProperty(column)) {
                text = toString(values[column]).toLowerCase();
                if ((searchString !== "") && (text.search(searchString) > -1)) {
                    return true;
                }
            }
            return false;
        },
        reset: function() {
            list.reset.search();
            list.searched = false;
        }
    };
    

    With

    if (typeof String.prototype.startsWith != 'function') {
        // see below for better implementation!
        String.prototype.startsWith = function (str) {
            return this.indexOf(str) === 0;
        };
    }
    var search = {
        list: function() {
            for (var k = 0, kl = list.items.length; k < kl; k++) {
                search.item(list.items[k]);
            }
        },
        item: function(item) {
            item.found = false;
            for (var j = 0, jl = columns.length; j < jl; j++) {
                if (search.values(item.values(), columns[j])) {
                    item.found = true;
                    return;
                }
            }
        },
        values: function(values, column) {
            if (values.hasOwnProperty(column)) {
                text = toString(values[column]).toLowerCase();
                if ((searchString !== "") && (text.startsWith(searchString))) {
                    return true;
                }
            }
            return false;
        },
        reset: function() {
            list.reset.search();
            list.searched = false;
        }
    };
    

    Explanation: I created startsWith function (found on stackoverflow) which checks whether the search string starts with the exact characters of a given sentence, and modified the values function of parent search block. and replaced its text.search() function with my startsWith function.