<textarea id="fulltext">The brown fox jumps over the white sheep. That is a quite foxy lady, but I am the only real fox in here. I used to think foxes were scary, until I saw a fox in my yard yesterday.</textarea>Match word:
<input type="text" id="match" value="fox" />
<input type="button" onclick="findWord()" value="Find word" />
<div id="output"></div>
function findWord() {
var outputDiv = $('#output');
var searchText = $('#fulltext').val();
var wordMatch = $('#match').val().toLowerCase();
if (wordMatch == '') {
alert('Please enter a word!');
return;
}
outputDiv.empty();
// Simple method: matches also partials ('fox' in 'foxy')
var m = searchText.match(new RegExp(wordMatch.toString().replace(/(?=[.\\+*?[^\]$(){}\|])/g, "\\"), "ig"));
outputDiv.append('<div>' + (m ? m.length : 0) + ' partial matches</div>');
// Advanced method: matched only full words.
// This one first splits the string in words, and then loops
// through all of them (potentially slow with very longs texts)
var count = 0;
var words = searchText.split(' ');
for (x in words) {
if (words[x].toLowerCase() == wordMatch) {
count++;
}
}
outputDiv.append('<div>' + count + ' full word matches</div>');
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.