How to remove index attribute from all elements in the variable container.innerHTML. Html is generated dynamically.
var container.innerHTML = test <a class ="test-image-class" index="0" href="" ><span>testimage</span></a>
<a class = "test-image-class" index="1" href="" ><span>testimage</span></a>
these two images are not matching <a class = "test-image-class" index="3" href="" ><span>testimage</span></a>
An easy solution can be just replace with regular expression
`test <a class ="test-image-class" index="0" href="" ><span>testimage</span></a>
<a class = "test-image-class" index="1" href="" ><span>testimage</span></a>
these two images are not matching <a class = "test-image-class" index="3" href="" ><span>testimage</span></a>  `.replace(/index="[\d]+"/g, '')
Here replace
is the string replace function for JavaScript it's generally accepts two parameter one search string or regex pattern & replace string. Here /index="[\d]+"/g
is the pattern ''
is the replace string. g
is used to match all.
A Regex is the easiest way out of this. The search below uses index
let str = `test <a class ="test-image-class" index="0" href="" ><span>testimage</span></a>
<a class = "test-image-class" index="1" href="" ><span>testimage</span></a>
these two images are not matching <a class = "test-image-class" index="3" href="" ><span>testimage</span></a>  `;
let re = /index="[^"]*"/gi;
let result = str.replace(re, "");
console.log(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.