簡體   English   中英

檢查字符串是否包含嚴格子字符串

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM