通过JS取出字符串格式的HTML属性值

导读:本篇文章讲解 通过JS取出字符串格式的HTML属性值,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

案例

let str = '<span data-dictId="0">123</span><span data-dictId="1">123</span><span data-dictId="2">123</span><span data-dictId="3">123</span><span data-dictId="4">123</span><span data-dictId="5">123</span><span data-dictId="6">123</span><span data-dictId="7">123</span><span data-dictId="8">123</span><span data-dictId="9">123</span><span data-dictId="10">123</span><span data-dictId="11">123</span><span data-dictId="12">123</span><span data-dictId="13">123</span><span data-dictId="14">123</span><span data-dictId="15">123</span><span data-dictId="16">123</span><span data-dictId="17">123</span><span data-dictId="18">123</span><span data-dictId="19">123</span><span data-dictId="20">123</span><span data-dictId="21">123</span><span data-dictId="22">123</span><span data-dictId="23">123</span><span data-dictId="24">123</span><span data-dictId="25">123</span><span data-dictId="26">123</span><span data-dictId="27">123</span><span data-dictId="28">123</span><span data-dictId="29">123</span><span data-dictId="30">123</span><span data-dictId="31">123</span><span data-dictId="32">123</span><span data-dictId="33">123</span><span data-dictId="34">123</span><span data-dictId="35">123</span><span data-dictId="36">123</span><span data-dictId="37">123</span><span data-dictId="38">123</span><span data-dictId="39">123</span>'
// 取出字符串中sapn标签的data-dictId属性的值
let attrAndValueArr = str.match(/ data-dictId=\"(.*?)\"/g) // 属性及值
//  [' data-dictId="0"', ' data-dictId="1"', ' data-dictId="2"', ' data-dictId="3"', ' data-dictId="4"', ' data-dictId="5"', ' data-dictId="6"', ' data-dictId="7"', ' data-dictId="8"', ' data-dictId="9"', ' data-dictId="10"', ' data-dictId="11"', ' data-dictId="12"', ' data-dictId="13"', ' data-dictId="14"', ' data-dictId="15"', ' data-dictId="16"', ' data-dictId="17"', ' data-dictId="18"', ' data-dictId="19"', ' data-dictId="20"', ' data-dictId="21"', ' data-dictId="22"', ' data-dictId="23"', ' data-dictId="24"', ' data-dictId="25"', ' data-dictId="26"', ' data-dictId="27"', ' data-dictId="28"', ' data-dictId="29"', ' data-dictId="30"', ' data-dictId="31"', ' data-dictId="32"', ' data-dictId="33"', ' data-dictId="34"', ' data-dictId="35"', ' data-dictId="36"', ' data-dictId="37"', ' data-dictId="38"', ' data-dictId="39"']
let valueArr = [] // 放所有该属性的值
for(let i = 0; i < attrAndValueArr.length;i++ ) {
    valueArr.push(attrAndValueArr[i].replace(/ data-dictId=/g,'').replace(/\"/g,''))
}
//  valueArr: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39']

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/64765.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!