How to Search a word "from a local file" using javascript

I have the html code to read a local file below:

<p><input id="file-input" type="file" /></p>
<p>Search keyword</p>
<p><input id="searchbar" spellcheck="true" type="text" /></p>
<pre id="file-content">No data yet.</pre>

which I would like to work with the search javascript given by Roko C. Buljan in this post here

16 thoughts on “How to Search a word "from a local file" using javascript”

  1. If you want to search in your file-content element then you can do it like below

    var src_str = $("#file-content").text();
    var term = $("#searchbar").val();
    term = term.replace(/(\s+)/,"(<[^>]+>)*$1(<[^>]+>)*");
    var pattern = new RegExp("("+term+")", "gi");
    src_str = src_str.replace(pattern, "<mark>$1</mark>");
    src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"$1</mark>$2<mark>$4");


Leave a Comment