10

JS使用replace()函数全部替换

 3 years ago
source link: http://www.z16388.top/2019/11/16/jsreplace/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JS使用replace()函数全部替换

2019-11-16

19

在处理爬虫爬取下来的数据时,遇到了在文字中出现了经过转义的换行符,在文中显示出了\n,很影响观赏效果。因此,我对内容做了处理。

但是在刷库的过程中,我发现,我总不能一次处理完所有的数据。后来发现是JavaScript的Replace函数的问题,这个函数默认只能替换第一个匹配到的项目。如果需要处理全部的,需要使用正则表达式:

string.replace(/\\n/g, "\n")

除此之外,下面是一些处理爬取内容常用操作,包含了html的转义:

string.replace(/ /g, ' ')
string.replace(/</g, '<')
string.replace(/>/g, '>')
string.replace(/&/g, '&')
string.replace(/"/g, '"')
string.replace(/=/g, '=')
string.replace(/\[.*?\]/g,'')
string.replace("\\n","\n")
string.replace("\\t","")
string.replace("\\r","")

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK