zzz

251 阅读1分钟

const HTML_SELF_CLOSING_TAG_PATTERN = /<("[^"]"|'[^']'|[^'">])\s/\s*>/ig; const HTML_TAG_PATTERN = /^<(\w*)(\s*.\s/\s*)?>$/;

const code = <view> <import src="../../common/head.wxml" /> <import src="../../common/foot.wxml" /> <comp1 :key={{fsa}} /> <comp2 :data={{fsa}} /> <input name=123 /> <template is="foot" /> <import src="../../common/head.wxml" /> fsdfdsf <view atrr=123>{{name}}</view> </view> function getClosingTagCode(code) { const closingTags = code.match(HTML_SELF_CLOSING_TAG_PATTERN); return [...new Set(closingTags)].reduce((lastCode, separator) => { const endTag = separator.match(HTML_TAG_PATTERN)[1]; return lastCode.split(separator).join(separator + </${endTag}>); }, code); }

getClosingTagCode(code);

//输出: fsdfdsf {{name}}

var closePattern = /<(--\s*)?("[^"]"|'[^']'|[^'">])\s/\s*>/gi;

var notClosePattern = /^<([0-9a-zA-Z-_])(\w)(\s*.\s/\s*)?>$/;

var codeSplit = "

Document test:sdfdsfdsxcv img:12142433😢
456
789<<<
";

function getCloseTagCode(code) { var closingTags = code.match(closePattern).filter(item => !item.includes(<!--)); return [...new Set(closingTags)].reduce((lastCode, separator) => { const endTag = separator.match(notClosePattern)[1]; return lastCode.split(separator).join(separator + </${endTag}>); }, code); }

getCloseTagCode(code);

目前bug: var code = <comp src='<comp2 />' /> <a href="<a href='a'/>"/></a> <a / > 1234 </a> getCloseTagCode(code); 16:20:01.761 " <a / > 1234 "