简体   繁体   中英

Check if a string contains a strict substring

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?

 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

Using Regex :

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

const regex = /\bhello\b/

regex.test(str1) // true
regex.test(str2) // false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM