// 自定义从控制台将变量保存到本地的函数;(function(console) {console.save = function(data, filename) {if (!data) {console.error("Console.save: No data")return}if (!filename) filename = "console.json"if (typeof data === "object") {data = JSON.stringify(data, undefined, 4)}var blob = new Blob([data], { type: "text/json" }),e = document.createEvent("MouseEvents"),a = document.createElement("a")a.download = filenamea.href = window.URL.createObjectURL(blob)a.dataset.downloadurl = ["text/json", a.download, a.href].join(":")e.initMouseEvent("click",true,false,window,0,0,0,0,0,false,false,false,false,0,null,)a.dispatchEvent(e)}})(console)const msg = 'hello world'console.save(msg, 'msg.txt')
tips:
如果要将变量作为对象key要加[]
let a = 'hhh'let b = 'jjj'c = {a: b} // {a: 'jjj'}c = {[a]: b} // {hhh:'jjj'}
JS用正则查找字符串的方法:
- re.exec(str)
- str.match(re)
tips:
python通过正则获取字符串
patten = re.compile(r'[^/]*\.jpg')image_name = patten.findall(image_url)[0]
