user1934286
user1934286

Reputation: 1762

I would like to make an advanced search with javascript. How?

I have a basic, case sensitive, term specific search with the code below. It will work for now but I would like something that (In order of importance):

1: ignores case (ie "hi" and "Hi" are both the same. toLowerCase is not an option and is not the same thing)

2: Will yield a hit if the search query is 'Search Term' and the searched string is 'searching terms', as an example.

3: Searches the entire string even after finding a hit for more hits.

The purpose is to search a <p> tag with a specific id for a term. If it has it then display it. Ultimately, I will use this in a loop that will search many <p> tags and display the ones with hits and leave hidden the ones without.

CODE:

<!DOCTYPE html>
<html>
    <body>
        <p id="demo">Click the button to locate where in the string a specifed value occurs.</p>
        <p id="demo1" style="display:none;">Hello world, welcome to the universe.</p>
        <button onclick="myFunction()">Try it</button>

        <script>
            function myFunction() {
                var x = document.getElementById("demo1")
                var str = x.innerHTML.toString();
                var n = str.indexOf("welcome");
                if (n != -1) {
                    x.style.display = 'inline';
                } else {
                    x.innerHTML = 'Negative';
                    x.style.display = 'inline';
                }
            }
        </script>

    </body>
</html>

Upvotes: 2

Views: 6988

Answers (3)

Blender
Blender

Reputation: 298402

I'd start by tokenizing your input string:

function tokenize(input) {
    return input.toLowerCase().replace(/[^a-z0-9_\s]/g, '').split(/\s+/g)
}

Which does this to your search terms:

> tokenize("I'm your search string.")
["im", "your", "search", "string"]

Next, strip off the suffixes (I'm not even going to try to handle the cases where this won't work. This is what NLP is for):

function remove_suffix(token) {
    return token.replace(/(ing|s)$/, '');
}

It'll do this to each token:

> remove_suffix('searching')
"search"
> remove_suffix('terms')
"term"

So for each query string, you can construct a list of keywords:

function get_keywords(query) {
    var tokens = tokenize(query);
    var keywords = tokens.map(remove_suffix);
    keywords.sort();

    return keywords;
}

And it will convert your query into keywords:

> get_keywords('searching terms')
["search", "term"]
> get_keywords('term search')
["search", "term"]

Now, you just check to see if your query string's keywords are contained within the keywords of your search string.

This is a really simple example and won't handle the multitude of corner cases, but at least you see somewhat how you can use keywords for searching.

Upvotes: 6

Steven
Steven

Reputation: 1375

This, with some tweaking, should fulfill your requirements I believe. It might be better to do this in the backend though =).

// returns the indices of the found searchStr within str, case sensitive if needed
function getIndicesOf(searchStr, str, caseSensitive) {
    var startIndex = 0, searchStrLen = searchStr.length;
    var index, indices = [];
    if (!caseSensitive) {
        str = str.toLowerCase();
        searchStr = searchStr.toLowerCase();
    }
    while ((index = str.indexOf(searchStr, startIndex)) > -1) {
        indices.push(index);
        startIndex = index + searchStrLen;
    }
    return indices;
}

// this splits the search string in an array of search strings
var myStringArray = mySearchString.split("\\s+");
var result = true;
// loop over all the split search strings, and search each seperately
for (var i = 0; i < myStringArray.length; i++) {
    var indices = getIndicesOf(myStringArray[i], "I learned to play the Ukulele in Lebanon.", false);
    if(indices && indices.length>0){
        // do something with the indices of the found string
    } else {
        result = false;
    }
}
// result will be false here if one of the search terms was not found.

borrowed from here

Upvotes: 2

Mark Bramnik
Mark Bramnik

Reputation: 42531

Take a look on Regular expressions engine. It take some time to learn but once you know it you'll probably achieve your goal here.

Here is a: link

Hope this helps

Upvotes: 0

Related Questions