0

Hey all i have found the following code that finds a word within the page:

JSFiddle & Original Page

function searchAndHighlight(searchTerm, selector) {
    if(searchTerm) {
        //var wholeWordOnly = new RegExp("\\g"+searchTerm+"\\g","ig"); //matches whole word only
        //var anyCharacter = new RegExp("\\g["+searchTerm+"]\\g","ig"); //matches any word with any of search chars characters
        var selector = selector || "body";                             //use body as selector if none provided
        var searchTermRegEx = new RegExp(searchTerm,"ig");
        var matches = $(selector).text().match(searchTermRegEx);
        if(matches) {
            $('.highlighted').removeClass('highlighted');     //Remove old search highlights
                $(selector).html($(selector).html()
                    .replace(searchTermRegEx, "<span class='highlighted'>"+searchTerm+"</span>"));
            if($('.highlighted:first').length) {             //if match found, scroll to where the first one appears
                $(window).scrollTop($('.highlighted:first').position().top);
            }
            return true;
        }
    }
    return false;
}

$(document).ready(function() {
    $('#search-button').on("click",function() {
        if(!searchAndHighlight($('#search-term').val())) {
            alert("No results found");
        }
    });
});

Within the code you can see it has var anyCharacter = new RegExp("\g["+searchTerm+"]\g","ig"); //matches any word with any of search chars characters.

However, when i try using that RegExp like so:

var searchTermRegEx = new RegExp("\\g["+searchTerm+"]\\g","ig");

it doesnt seem to return any results then even if i type in an exact name.

Any help would be great!

1

2 Answers 2

1

This fiddle works.

Not sure what the original authors were thinking with the \\g stuff.

The key is this regex:

      searchRegex   = new RegExp('(\\b)(' + searchTerm + ')(\\b)','ig');
Sign up to request clarification or add additional context in comments.

5 Comments

Not sure your fiddle works? If you type in ace it says it can not find it.. even though space has ace in it.
Maybe im looking at this the wrong way.. I am looking for a type of wildcard like you would find in a SQL Query %whateverHere%.
Ah, I misunderstood. That solution only finds whole words. Gimme a sec to match partials.... See jsfiddle.net/wmca4/5
i have also noticed that if you searched once and then try searching again it does nothing?
Yes, the fiddle is limited to a single search. I am quite sure it is the same with the original fiddle.
0

To match any word, try

/^\b\w+\b$/i

The regexp matches multiple characters between word boundaries

Replace

var searchTermRegEx = new RegExp("\\g["+searchTerm+"]\\g","ig");

with

var searchTermRegEx = /^\b\w+\b$/i;

The difference is that we are using regex literal than using regex object.

3 Comments

Start and end characters plus global flag doesn't make much sense here.
Had a confusion whether or not to include it. Fixed.
@aliasm2k Where would that go in the code in my OP? Can you use that with the code example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.