-2

I have the next situation:
I want to check if in const str = "hello world" is the word world, not wor, not worl, but strict world. I know that exists includes() method, but it is not working as i described.
How to solve the question?

Asking
  • 3,487
  • 11
  • 51
  • 106
  • 2
    `"hello world".includes("hello")` is `true` while `"hell world".includes("hello")` is `false`. – VLAZ Nov 06 '20 at 09:56
  • @VLAZ maybe OP also mean to "hello worldo" which includes return true. – Mosh Feu Nov 06 '20 at 09:58
  • @VLAZ, why `str.includes("wor");` is true? – Asking Nov 06 '20 at 09:59
  • 3
    Does this answer your question? [whole word match in javascript](https://stackoverflow.com/questions/2232934/whole-word-match-in-javascript) – Ivar Nov 06 '20 at 10:00
  • @Asking because the substring exists. Your question wasn't at all clear about this. The situation you described in the question didn't suffer the problem you described. – VLAZ Nov 06 '20 at 10:01
  • @MoshFeu `"hello worldo".includes("hello")` is still *correctly* `true`, and `"hell worldo".includes("hello")` is *correctly* `false`. – VLAZ Nov 06 '20 at 10:05
  • Yes but `"helloooo worldo".includes("hello")` is true – Alexandre Nov 06 '20 at 10:13

2 Answers2

-1

const str = "hello world"
const sub1 = "world"
const sub2 = "wor"

let strictSub = (a, b) => a.split(' ').includes(b)

console.log(strictSub(str, sub1))
console.log(strictSub(str, sub2))

This would work

bli07
  • 673
  • 1
  • 5
  • 16
-3

Using Regex :

const str1 = "hello world"
const str2 = "helloooo world"

const regex = /\bhello\b/

regex.test(str1) // true
regex.test(str2) // false
Alexandre
  • 1,940
  • 1
  • 14
  • 21