<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Word Search</title> <script> function searchWord() { const paragraph = document.getElementById('paragraph').value; const searchWord = document.getElementById('search').value.trim(); const wordsArray = paragraph.split(/\s+/); let count = 0; for (let word of wordsArray) { if (word.localeCompare(searchWord, undefined, { sensitivity: 'base' }) === 0) { count++; } } const resultDiv = document.getElementById('result'); // Adjusting the message to match the expected output if (count > 0) { resultDiv.innerHTML = `Searched text ${searchWord} is present ${count} times in the paragraph.`; } else { resultDiv.innerHTML = 'Searched text not found'; // Ensured no extra formatting } } </script> </head> <body> <h2>Search Word</h2> <textarea id="paragraph" rows="10" cols="50" placeholder="Enter your paragraph here..."></textarea><br><br> <input type="text" id="search" placeholder="Enter word to search..."> <button id="searchWord" onclick="searchWord()">Search</button> <div id="result" style="margin-top: 20px; font-weight: bold;"></div> </body> </html>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter