[英]Check if a string contains a strict substring
我有下一个情况:
我想检查const str = "hello world"
是否是单词world
,不是wor
,不是worl
,而是严格的world
。 我知道存在includes()
方法,但它不像我描述的那样工作。
如何解决问题?
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))
这会工作
使用正则表达式:
const str1 = "hello world"
const str2 = "helloooo world"
const regex = /\bhello\b/
regex.test(str1) // true
regex.test(str2) // false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.