The forums on sitepoint are excellent
If my specific search text is “site”, I need to return the entire word related to it. In this case, “sitepoint”.
Any idea how I could do this?
I know that there are users here who will lambaste me for suggesting it, but…
Regular expressions can find it.
var str = "The forums on sitepoint are excellent. This site is good.";
var restr = document.getElementById('restr');
restr.textContent = "str = " + str + "\
var pattern = /\\b[\\w\\d]*site[\\w\\d]*\\b/gi;
var wordFind = pattern.exec(str);
restr.textContent = restr.textContent + "FIND: " + wordFind;
This will find the first word that contains ‘site’. If you want only the first word that BEGINS with ‘site’, then remove the first [\w\d]* from the pattern.