1. // 自定义从控制台将变量保存到本地的函数
    2. ;(function(console) {
    3. console.save = function(data, filename) {
    4. if (!data) {
    5. console.error("Console.save: No data")
    6. return
    7. }
    8. if (!filename) filename = "console.json"
    9. if (typeof data === "object") {
    10. data = JSON.stringify(data, undefined, 4)
    11. }
    12. var blob = new Blob([data], { type: "text/json" }),
    13. e = document.createEvent("MouseEvents"),
    14. a = document.createElement("a")
    15. a.download = filename
    16. a.href = window.URL.createObjectURL(blob)
    17. a.dataset.downloadurl = ["text/json", a.download, a.href].join(":")
    18. e.initMouseEvent("click",true,false,window,0,0,0,0,0,false,false,false,false,0,null,)
    19. a.dispatchEvent(e)
    20. }
    21. })(console)
    22. const msg = 'hello world'
    23. console.save(msg, 'msg.txt')

    tips:
    如果要将变量作为对象key要加[]

    1. let a = 'hhh'
    2. let b = 'jjj'
    3. c = {a: b} // {a: 'jjj'}
    4. c = {[a]: b} // {hhh:'jjj'}

    JS用正则查找字符串的方法:

    1. re.exec(str)
    2. str.match(re)

    tips:
    python通过正则获取字符串

    1. patten = re.compile(r'[^/]*\.jpg')
    2. image_name = patten.findall(image_url)[0]