用HTML标记替换HTML结束标记+ \ n分析(Replace HTML closing tags with HTML tags + \n Analysis)

编程入门 行业动态 更新时间:2024-10-23 13:36:33
用HTML标记替换HTML结束标记+ \ n分析(Replace HTML closing tags with HTML tags + \n Analysis)

我需要用HTML标签+ \ n替换HTML结束标签。 我找到了这个答案

var regex = new RegExp("(</.*?>)", "gi"); strContent = strContent.replace(regex, "$1 \n ") .replace(/\/>/g,'/> \n ');

有人可以向我解释上面的代码中发生了什么吗? 为什么宣布正则表达式被替换? 每个部分意味着什么?

I need to replace HTML closing tags with HTML tags + \n. I've found this SO answer

var regex = new RegExp("(</.*?>)", "gi"); strContent = strContent.replace(regex, "$1 \n ") .replace(/\/>/g,'/> \n ');

Could someone explain to me what is happening in the above code? Why is a regex declared then replaced? What does each part mean?

最满意答案

第一个正则表达式(</.*?>)正在搜索表单</span>或</div>所有结束标记,并选择标记作为一个组。 此组将替换为找到的组以及新行。

第二个正则表达式, /\/>/正在查找格式为<img />或<input />的最后两个字符。 \正在逃离第二个/ 。 然后用相同的字符和新行替换这两个字符。

The first regex (</.*?>) is searching for all closing tags of the form </span> or </div> and selecting the tag as a group. This group is replaced with the group as found plus a new line.

The second regex, /\/>/ is looking for the last two characters of tags that are of the form <img /> or <input />. The \ is escaping the second /. Then those two characters are replaced with the same characters plus a new line.

更多推荐

本文发布于:2023-04-29 11:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336385.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   结束   HTML   Replace   Analysis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!