 <pre><code>function getWordAt(str, pos) { // Perform type conversions. str = String(str); pos = Number(pos) &gt;&gt;&gt; 0; // Search for the word's beginning and end. var left = str.slice(0, pos + 1).search(/\S+$/), right = str.slice(pos).search(/\s/); // The last word in the string is a special case. if (right &lt; 0) { return str.slice(left); } // Return the word, using the located bounds to extract it from the string. return str.slice(left, right + pos); } </code></pre> <p>This function <strong>accepts any whitespace character</strong> as a word separator, including spaces, tabs, and newlines. Essentially, it looks:</p> <ul> <li>For the beginning of the word, matched by <code>/\S+$/</code></li> <li>Just past the end of the word, using <code>/\s/</code></li> </ul> <p>As written, the function will return <code>""</code> if the index of a whitespace character is given; spaces are not part of words themselves. If you want the function to instead <strong>return the preceding word</strong>, change <code>/\S+$/</code> to <code>/\S+\s*/</code>.</p> <hr> <p>Here is some example output for <code>"This is a sentence."</code></p> <pre><code>0: This 1: This 2: This 3: This 4: 5: is 6: is 7: 8: a 9: 10: sentence. // ... 18: sentence. </code></pre> <hr> <p>Modified to return the preceding word, the output becomes:</p> <pre><code>0: This 1: This 2: This 3: This 4: This 5: is 6: is 7: is 8: a 9: a 10: sentence. // ... 18: sentence. </code></pre>