6

例如,我有一个字符串“this=20is=20a=20string”,我想将其转换为“this is a string”。

是否有一个库函数或一个 npm 模块可以做到这一点,或者我应该创建自己的函数来做到这一点?

4

2 回答 2

8

使用 mimelib:

var mimelib = require("mimelib");
mimelib.decodeQuotedPrintable("this=20is=20a=20string") === "this is a string"
mimelib.decodeMimeWord("=?iso-8859-1?Q?=27text=27?=") === "'text'"
于 2012-11-01T04:34:47.200 回答
0

function decodeQuotedPrintable(data)
{
    // normalise end-of-line signals 
    data = data.replace(/(\r\n|\n|\r)/g, "\n");
        
    // replace equals sign at end-of-line with nothing
    data = data.replace(/=\n/g, "");

    // encoded text might contain percent signs
    // decode each section separately
    let bits = data.split("%");
    for (let i = 0; i < bits.length; i ++)
    {
        // replace equals sign with percent sign
        bits[i] = bits[i].replace(/=/g, "%");
        
        // decode the section
        bits[i] = decodeURIComponent(bits[i]);
    }
        
    // join the sections back together
    return(bits.join("%"));
}

于 2022-01-15T18:43:59.187 回答