简体   繁体   English

如何删除某些空的新行/空格

[英]How to remove certain empty new lines/blank spaces

So I'm learning web scraping on my test store and I'm not sure how to properly remove empty new lines from the 'sizes' array.所以我在我的测试商店学习 web 刮擦,我不确定如何从“大小”数组中正确删除空的新行。

 const $ = cheerio.load(body)

            $('div.listing').each((i, listing) => {

                let sizes = []

                let productUrl = $(listing).find('a').attr('href')
                let productTitle = $(listing).find('a').attr('title').toUpperCase()
                let productSizes = $(listing).find('.size-cont').text()

                if (products.indexOf(productUrl) == -1) {

                    sizes.push(productSizes)
                    console.log(sizes.join('').replace(/[^\S\r\n]+/g,"").trim()) // issue here

                }

            })

Current output:当前 output:

9.0




9.5




10.0




10.5

I want it to be:我希望它是:

9.0
9.5
10.0
10.5

You meant this?你是说这个吗?

 console.log( `9.0 9.5 10.0 10.5`.replace(/(\s)+/g,"$1") )

Try the following尝试以下

const output = sizes.join('').replace(/[^\S\r\n]+/g,"").trim();
const noWhitespaceOutput.replace(" ", "");
console.log(noWhitespaceOutput);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM