How to find a word in a string that begins with specific text?

Hello.

I need to use javascript/jquery to find an entire word inside a string, that begins with specific text. For example, here’s a sample string:

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?
Thanks

I know that there are users here who will lambaste me for suggesting it, but…

Regular expressions can find it.
<div id=“restr”></div>


    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.

HTH,

:slight_smile: