Javascript: find word in string

橙三吉。 提交于 2019-11-28 12:31:43

You can use split and some:

function findWord(word, str) {
  return str.split(' ').some(function(w){return w === word})
}

Or use a regex with word boundaries:

function findWord(word, str) {
  return RegExp('\\b'+ word +'\\b').test(str)
}

No there is not a built in function for this. You will have to add programming such as a regex or split() it by whitespace then compare the result == 'test'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!