Js-正则-正则表达式 已知前后文 取中间文本

已知前后文本获取中间文本

复制代码
/**
 * 已知前后文 取中间文本
 * @param str 全文
 * @param start 前文
 * @param end 后文
 * @returns 中间文本 || null
 */
getStr(str, start, end) {
    let res = str.match(new RegExp(`${start}(.*?)${end}`))
    return res ? res[1] : null
}
复制代码

使用:

// tar.name = 'buy4H'
let hours = this.getStr(tar.name, 'buy', 'H');
// 输出结果: hours ==> 4

 

posted @   前端法师  阅读(7315)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示