我需要用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.
更多推荐
发布评论