已知前后文本獲取中間文本
/** * 已知前后文 取中間文本 * @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