如何使用JavaScript循环遍历长字符串以在每次匹配后插入新字符串 [英] How to use JavaScript to loop through a long string to insert a new string after every match
问题描述
我有一个包含表示XML文档的长字符串的变量。在该字符串中,我需要搜索每个自动关闭标记并扩展为两个匹配的开始/结束标记。我真的不确定如何解决这个问题,并希望得到你的建议。在这一点上,我所知道的是如何通过正则表达式匹配自动关闭标签: [^<] +?/>
以下是我想要的一个简短示例喜欢完成:
I have a variable that contains a long string that represents an XML document. Within that string, I need to search for every self-closing tag and expand into two matching opening/closing tags. I'm really not sure how to tackle this and would appreciate your advice. At this point, all I know is how to match a self-closing tag via regex: [^<]+?/>
Here's a short example of what I would like to accomplish:
ORIGINAL STRING:
ORIGINAL STRING:
<outer-tag>
<inner-tag-1>
<SELF-CLOSING-TAG-1 foo="bar"/>
<SELF-CLOSING-TAG-2/>
</inner-tag-1>
<inner-tag-2>
<SELF-CLOSING-TAG-3 attr="value"/>
</inner-tag-2>
</outer-tag>
MODIFIED STRING:
MODIFIED STRING:
<outer-tag>
<inner-tag-1>
<SELF-CLOSING-TAG-1 foo="bar"></SELF-CLOSING-TAG-1>
<SELF-CLOSING-TAG-2></SELF-CLOSING-TAG-2>
</inner-tag-1>
<inner-tag-2>
<SELF-CLOSING-TAG-3 attr="value"></SELF-CLOSING-TAG-3>
</inner-tag-2>
</outer-tag>
推荐答案
我使用了 w3规范创建正则表达式,正确解析格式良好的XML中的标签。
I have used the w3 specifications to create a regexp which correctly parses tags in well-formed XML.
首先,选择定义起始标签的字符(按规格)。然后,匹配剩余的字符,不包括可能尾随间距和 />
。通过
<全局替换匹配的子串+ starttag +剩余+>< /+ starttag +>
。见下文:
First, select the characters which define the start-tag (per specs). Then, match the remaining characters, excluding possibly trailing spaced and />
. Globally replace the matched substrings by"<" + starttag + remaining + "></" + starttag + ">"
. See below:
//According to the W3 spec:
var pattern = /<([:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD][-.0-9\xB7\u0300-\u036F\u0203F-\u2040]*)([^>]*?)\s*?\/>/g;
string.replace(pattern, '<$1$2></$1>');
这篇关于如何使用JavaScript循环遍历长字符串以在每次匹配后插入新字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!