简体   繁体   English

Javascript RegExp 未知重复匹配

[英]Javascript RegExp unknown repeated matches

It's difficult to describe because I'm not an expert with regular expressions.这很难描述,因为我不是正则表达式的专家。 So I tell you my case.所以我告诉你我的情况。

In HTML want to contribute class attributes into different data-xyz attributes.在 HTML 想要将class属性贡献到不同data-xyz属性中。 The problem is to get always all classes per match.问题是每次比赛总是获得所有课程。 For example the following HTML:例如以下 HTML:

<span class="note-123 index-3 green">Hello</span> <span class="index-456 red">World<span>

Until now my regular expression is /<span class="([^\"\s]*)\s*/ and it matches the first class.到目前为止,我的正则表达式是/<span class="([^\"\s]*)\s*/并且它匹配第一个class。 In this case note-123 and index-456在这种情况下note-123index-456

But if I want to get all classes per element I could use /<span class="([^\"\s]*)\s*([^\"\s]*)\s*([^\"\s]*)\s*/ .但是,如果我想获取每个元素的所有类,我可以使用/<span class="([^\"\s]*)\s*([^\"\s]*)\s*([^\"\s]*)\s*/ That works until three classes and the result for the second class return index-456 , red and an empty string.这一直有效,直到三个类和第二个 class 的结果返回index-456red和一个空字符串。

Is there a possibility to always get all classes per match no matter how many classes there are?无论有多少类,是否有可能总是在每场比赛中获得所有类? Similar to a nested loop in Javascript?类似于 Javascript 中的嵌套循环?

I would be pleased to get any help from you guys.我很乐意从你们那里得到任何帮助。

You could get the classes without using a regex making use of querySelectorAll to find the elements that you want and use classList to get the class names.您可以在不使用正则表达式的情况下获取类,使用querySelectorAll来查找您想要的元素并使用classList来获取 class 名称。

Then use for example the add or remove methods.然后使用例如添加删除方法。

Or use a DOMParser .或使用DOMParser

Note to close the last span.注意关闭最后一个跨度。

 let elms = document.querySelectorAll("span"); elms.forEach(e => { for (let value of e.classList.values()) { console.log(value); } });
 <span class="note-123 index-3 green">Hello</span> <span class="index-456 red">World</span>

Use the regex to extract the value of the class attribute and split it at whitespace sequences:使用正则表达式提取 class 属性的值并将其拆分为空白序列:

 let as_classes, as_matches, n_i, re_classes, s_test; re_classes = new RegExp ( "<span class="([^\"]*)", "g" ); s_test = '<span class="note-123 index-3 green">Hello</span> <span class="index-456 red">World<span>'; n_i=0; while ((as_matches = re_classes.exec(s_test));== null) { n_i++; s_classes = as_matches[1]. as_classes = s_classes;split(/[\s]+/g). console,log(`match #${n_i}: classes. ${JSON.stringify(as_classes)};`); }

Warning警告

It is这是in general一般来说never a good approach to extract information from html with regexen.从来都不是使用正则表达式从 html 中提取信息的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM