我如何从 cheerio 中删除数据 return 中的“\n”

How i can to remove "\n" in data return from cheerio

我正在尝试使用 NodeJS 和 Cheerio 从网站获取图像。返回的图像 URL 前面有 \n。我怎样才能删除 \n

这是代码:

$("div[class=xxxx] img").attr("src")

但是 Cheerio returns \nhttp://www.xxxxx.com/example.jpg

我尝试使用:

var image = $("div[class=xxxx] img").attr("src").replace(\n,"")
var image = $("div[class=xxxx] img").attr("src").toString().replace(\n,"")

但是不行。

\n 是换行符。您可以使用 String.prototype.trim() 函数将其删除。

尝试以下操作:

$("div[class=xxxx] img").attr("src").trim();

下面是唯一对我有用的

.replace(/\n/g,'')