eve*_*veo 0 html javascript css regex jquery
我有一个文本区域,可以在其中粘贴一段 HTML 代码。当我点击提交时,我想从该块中提取所有 CSS 类和 ID,并将它们放入一个数组中。
到目前为止,我已经提交工作以及正则表达式,但我不知道如何过滤整个块并提取文本与正则表达式匹配的所有实例。
索引.html
<body>
<textarea id="codeInput"></textarea>
<button id="submitCode">submit</button>
<script src="functions.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
函数.js
$(function() {
$('#submitCode').click(function() {
var codeInput = $('textarea#codeInput').val();
console.log(codeInput);
});
});
Run Code Online (Sandbox Code Playgroud)
$('#submitCode').click(function() {
var codeInput = $('textarea#codeInput').val();
var codeHTML = $('<div>', { html: codeInput }); // Parse the input as HTML
var allIds = [];
var allClasses = [];
codeHTML.find('[id]').each(function() {
allIds.push(this.id);
});
codeHTML.find('[class]').each(function() {
allClasses = allClasses.concat(this.className.split(' '));
});
console.log("IDs: " + allIds.join(', '));
console.log("Classes: " + allClasses.join(', '));
});
Run Code Online (Sandbox Code Playgroud)