【问题标题】:Why i am getting null even 188 is present in string为什么即使字符串中存在 188 我也会得到空值
【发布时间】:2022-06-30 14:45:57
【问题描述】:

const str = '1881P'
let searchText = "188"
searchText = new RegExp(`\\b${searchText}\\b`,'ig');
console.log(str.match(searchText))

当 str 中存在 188 时,为什么我在控制台中为空

【问题讨论】:

  • 因为 \b 不匹配 1

标签: javascript regex


【解决方案1】:

如果您想将188 匹配为大字符串中的子字符串,则删除单词边界:

const str = '1881P'
let searchText = "188"
searchText = new RegExp(`${searchText}`, 'ig');
console.log(str.match(searchText))

【讨论】:

    【解决方案2】:

    const str = '1881P'
    let searchText = "188"
    searchText = new RegExp(`${searchText}`,'ig');
    console.log(str.match(searchText))

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2021-01-14
      • 1970-01-01
      相关资源
      最近更新 更多