<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>lcx</title> </head> <body> html: <textarea style=" 600px;height: 300px;font-size: 20px;" class="userhtml" ></textarea> <button onclick="html2jsonstr();">html2jsonstr</button> <div class="result" /> <script> var h = 'svg width="100%" height="100%" viewBox="0 0 370 490"'; var leve1 = " "; var leve2 = " "; function html2jsonstr(){ var userhtml = document.querySelector(".userhtml").value; var result = document.querySelector(".result"); var json = "{<br/>"; var patt2=/[A-Za-z]+/; var name = patt2.exec(userhtml); json = json + leve1 + "name: "" + name + "",<br/>"; json = json + leve1 + "attrs: {<br/>"; patt2=/([A-Za-z]+)=("[^"]+")/g; var data = {}; while(data = patt2.exec(userhtml)){ json = json + leve2 + data[1] + ": " + data[2] + ",<br/>"; } json = json + leve1 + "}<br/>"; json = json + "}<br/>"; result.innerHTML = json; } function html2json(){ var h = document.querySelector(".userhtml"); var json = {}; var patt2=/[A-Za-z]+/; var name = patt2.exec(h); json.name = name; json.attrs = {}; patt2=/([A-Za-z]+)="([^"]+)"/g; var data = {}; while(data = patt2.exec(h)){ json.attrs[data[1]] = data[2]; } console.log(json); } </script> </body> </html>