Use split and some function or regex with word boundaries to find or Extract a specific word from string in JavaScript.
Get word from string JavaScript Example
HTML example code: Match if word present in given string.
Using split and some function
If word found then return true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE HTML> <html> <body> <script> var str = "Remove the last word."; function findWord(word, str) { return str.split(' ').some(function(w){return w === word}) } console.log(findWord("last",str)); </script> </body> </html> |
Using a regex with word boundaries:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE HTML> <html> <body> <script> var str = "Remove the last word."; function findWord(word, str) { return RegExp('\\b'+ word +'\\b').test(str) } console.log(findWord("last",str)); </script> </body> </html> |
Output:

Do comment if you have any doubts and suggestions on this JS strings topic.
Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
‘use strict’
let str=’Remove the last word.’
const findWord = (word) =>{
return str.split(‘ ‘).some((w)=>w===word)===true?
The word '${word}' is found
:The word '${word}' is not found
}
console.log(findWord(‘last’))