---
title: "jsoup之文本过滤"
author: "小网客"
date: "2015-07-16T10:37:15Z"
tags: []
categories: ["编程语言"]
slug: "jsoup"
draft: false
lastmod: "2015-07-16T10:37:15Z"
id: 2227800
url: "https://snv.iteye.com/blog/2227800"
---

背景：

基于jsoup(v 1.6.3)解析出来的网页内容进行过滤不需要的内容比如&lt;script&gt;

实现：

一种方式是基于tag的白名单，这种方式明显没有黑名单合适，不过jsoup木有提供黑名单功能

直接基于正则，常用的如下：

如：过滤&lt;script&gt;

```
String reg = "<\\s*?script[^>]*?>[\\s\\S]*?<\\s*?/\\s*?script\\s*?>";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(content.html());
articleVo.setContent(matcher.replaceAll(""));
```