Javascript Regex - 从字符串 html 获取 div ID

标签 javascript regex ecmascript-6

考虑以下代码:

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/

相关文章:

javascript - 使用 JSON Api URL EmberJS

html - 提取标签属性的正则表达式

java - 此正则表达式错误 : var worksheet = (. *)\\};

javascript - 未捕获错误 :expected a string. 您忘记从其定义的文件中导出您的组件,或者您可能混淆了默认/命名导入

node.js - string.includes ('a' ) 函数未在 Node.js 0.12 中定义?

javascript - 我将如何使用 Javascript 将计时器添加到轮播 slider

javascript - 与 javascript 正则表达式完全匹配的字符串

JavaScript 正则表达式 - 奇怪的结果

javascript - 为什么更改数组的副本会影响原始数组?

javascript - Polymer 1.X - 复杂的观察者不开火