简体   繁体   English

检查字符串是否包含严格子字符串

[英]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 .我想检查const str = "hello world"是否是单词world ,不是wor ,不是worl ,而是严格的world I know that exists includes() method, but it is not working as i described.我知道存在includes()方法,但它不像我描述的那样工作。
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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