考虑以下代码:
const content = "<div id='x'><h2>Lorem Ipsum</h2> <p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p></div>"
let ids = content.match(/id='(.*?)'/g).map((val) => {
return val.replace(/id='/g,'');
})
当我执行 console.log(ids)
时 - 我得到像这样的输出 'x\''
但我只需要 'x'
。我想知道如何实现这个结果,我尝试过随机更改正则表达式符号,因为我根本不知道正则表达式 - 而且似乎没有任何效果。
请不要使用 jquery。预先感谢您
最佳答案
const content = "<div id='x'><h2>Lorem Ipsum</h2> <p>is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p></div>"
let ids = content.match(/id='(.*?)'/g).map((val) => {
return val.replace(/id='/g,'').replace("'",'');
})
console.log(ids)
关于Javascript Regex - 从字符串 html 获取 div ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44923272/