美文网首页
提取一串字符串中的固定字符串

提取一串字符串中的固定字符串

作者: 我背井离乡了好多年 | 来源:发表于2021-06-09 15:32 被阅读0次
let theArr = `<p>&nbsp;</p>
<p>123<img src="http://10.10.0.99:8000/api/file/getFile/news/top.png" alt="" width="200" height="200" /><a title="004.xlsx" href="theoryOfLearning/004.pdf">theoryOfLearning/004.pdf</a></p>`
// console.log(theArr)

theArr = "111000222"


function getInnerString(source, prefix, postfix) {
  let regexp = new RegExp(encodeReg(prefix) + '.+' + encodeReg(postfix), 'gi');
  let matches = String(source).match(regexp);
  let tppArr = matches[0]
  tppArr = tppArr.replace(prefix, "")
  tppArr = tppArr.replace(postfix, "")
  return tppArr
}

//转义影响正则的字符
function encodeReg(source) {
  return String(source).replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1');
}

let result = getInnerString(theArr, "111", "222")
// 拿到中间的字符串
console.log(result)

相关文章

网友评论

      本文标题:提取一串字符串中的固定字符串

      本文链接:https://www.haomeiwen.com/subject/qlpeeltx.html