国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

javascript - HTML字符串排版

瀏覽:83日期:2022-12-06 16:06:51

問題描述

我現在有HTML代碼的字符串,想要對字符串進行排版,有沒有什么可用的方法?

需要一款js插件,或者方法,能在項目里面轉換用的。

比如原字符串是:

<p><p>This is a p</p><p>This is another p</p></p>

添加空格和換行,變成新的字符串:

<p> <p>This is a p</p> <p>This is another p</p></p>

問題解答

回答1:

第一種方案想用正則做答沒成功,沒有成功。整理了下思路,已經完美解決(自認為完美)。

function codeFormat(code, indent, tmpIndent){ var indent = indent || ’ ’; var tmpIndent = tmpIndent || ’n’; var preg = /<(S*)([^>]*)>([sS]*?)</1>/ig; return code.replace(preg, function($0, $1, $2, $3){return tmpIndent + ’<’ + $1 + $2 + ’>’ + codeFormat($3, indent, tmpIndent + indent) + ( $3.trim().substr(0,1) == ’<’ ? tmpIndent : ’’) + ’</’ + $1 + ’>’; });}codeFormat('<p><p>This is a p</p><p>This is anothers p</p></p><p><p>This is a p</p><p>This is another p</p></p>');/*<p> <p>This is a p</p> <p>This is anothers p</p></p><p> <p>This is a p</p> <p>This is another p</p></p>*/codeFormat(’<html><head><title></title></head><body class='haha'><p style='background:#C00;'><p>This is a p</p><p>This is another p</p></p></body></html>’, ’----’);/*<html>----<head>--------<title></title>----</head>----<body class='haha'>--------<p style='background:#C00;'>------------<p>This is a p</p>------------<p>This is another p</p>--------</p>----</body></html>*/

以下是循環的老答案:

/* 感覺沒什么難度,一個循環 遇見 >后跟<,換行 遇見 < 縮進 遇見 </ 取消縮進*/function codeFormat(code, indent){ var indent = indent || ' '; //縮進字符 var tmpIndent = ''; //保存代碼字符串 var result = '', key = '', keyNext = ''; for( var i = 0 ; i < code.length ; i++ ){key = code[i];keyNext = i < code.length-1 ? code[i+1] : '';if(key == '<'){ if( keyNext == '/' ){tmpIndent = tmpIndent.substr(indent.length); } if( result[result.length-1] == 'n' ){result += tmpIndent; } if( keyNext != '/' ){tmpIndent += indent; }}result += key;if(key == '>' && keyNext == '<' ){ result += 'n';} } return result;}codeFormat('<p><p>This is a p</p><p>This is another p</p></p>');/*<p> <p>This is a p</p> <p>This is another p</p></p>*/codeFormat(’<html><head><title></title></head><body class='haha'><p style='background:#C00;'><p>This is a p</p><p>This is another p</p></p></body></html>’, ’ ’);/*<html> <head><title></title> </head> <body class='haha'><p style='background:#C00;'> <p>This is a p</p> <p>This is another p</p></p> </body></html>*/回答2:

為什么我這個問題一直被踩?還是一個不錯的問題的嘛。。。。@mqycn , @zhenguoli

這是朋友@candy寫的一個方案:

import _ from ’lodash’;const splitOnTags = str => str.split(/(</?[^>]+>)/g).filter(line=>line.trim()!='');const isTag = str => /<[^>!]+>/.test(str);const isClosingTag = str => /</[^>]+>/.test(str);const isSelfClosingTag = str => /<[^>]+/>/.test(str);const isOpeningTag = str => isTag(str) && !isClosingTag(str) && !isSelfClosingTag(str);export default (str, indent) => { let depth = 0; indent = indent || ’ ’; return splitOnTags(str).map(item=>{ if(isClosingTag(item)) { depth--; } const line = _.repeat(indent, depth) + item; if(isOpeningTag(item)) { depth++; } return line; }).join(’n’);}回答3:

http://tool.oschina.net/codef... 在線的。其他的編輯器里面有beautify

javascript - HTML字符串排版

回答4:

對開發工具而言,你可以嘗試 VSCode 編輯器。

https://code.visualstudio.com/

安裝完成后,按 shift-ctrl-x 打開插件列表,搜索 Beautify 并安裝,即可實現 HTML 代碼美化。相應設置參考這里:

https://marketplace.visualstu...

在代碼中進行格式轉換可以使用 HTML String Parser,例如

https://www.npmjs.com/package...

或者使用 cheerio 解析 DOM 文本后重新輸出亦可。

3.(重新制作一個 HTML Parser 并不困難,參考我的專欄http://ewind.us/2016/toy-html...

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 成人欧美精品大91在线 | 老头做爰xxxx视频 | 香蕉久久久久 | 国产一级做a爰片久久毛片99 | 国产精品久久久久久久久99热 | 国产视频软件在线 | 手机看片手机在线看片 | 一级毛片免费观看视频 | 日韩一区三区 | 亚洲国产精品欧美日韩一区二区 | 久久福利青草精品资源站 | 美女张开腿让男人捅爽 | 午夜爽爽爽 | 国产美女在线一区二区三区 | 看真人视频a级毛片 | 成年人网站免费视频 | 欧美综合一区 | 淫模| 欧美精品成人一区二区视频一 | 欧美午夜不卡在线观看最新 | 狠狠色婷婷丁香综合久久韩国 | 免费特黄一级欧美大片 | 在线成人免费观看国产精品 | 中国内地毛片免费高清 | 性日韩精品 | 加勒比色 | 天天se天天cao综合网蜜芽 | 成人免费看www网址入口 | 在线a人片免费观看国产 | 在线播放成人毛片免费视 | 国产成人免费不卡在线观看 | 九九九九在线精品免费视频 | 美女张开腿让男人桶下面 | 九九精品视频一区在线 | 久久99精品久久久久久久野外 | 韩日毛片 | 青青热久久国产久精品 | 狠狠色狠狠色综合 | 日韩欧美特级毛片 | 手机看片国产欧美日韩高清 | 一区国严二区亚洲三区 |