㈠ 怎麼使用js過濾html標簽
你可以利用正則表達式來剔除這些標簽,也就是將所有的html類的標簽都替換為空即可:
//去除HTML標簽
str=str.replace(/</?[^>]*>/g,'');
㈡ 【Java作業向】正則表達式過濾HTML標簽
過濾HTML標簽的Java正則表達式 (?s)<.*?/?.*?>
按照你的要求編寫的用正則表達式過濾HTML標簽的Java程序回如下
public class AA {
public String tagFilter(String s){
String regex = "(?s)<.*?/?.*?>";
String ss=s.replaceAll(regex,"");
return ss;
}
public static void main(String[] args) {
String s="<div class="guid time online">測試答 abc</div><span data-url="games/details/" class="guid done">你好13548</span><a href="games/details/" class="guid">15個字母Abc</a><i class="icon-guid"/>";
String result=new AA().tagFilter(s);
System.out.println(result);
}
}
㈢ Java中怎麼樣能過濾掉html中的javascript
一般的解決辦法是將引號轉換成全形的。
這樣javascript代碼就不能夠正常運行了。。。
㈣ java 如何去除html中的一個指定標簽和指定標簽里的內容
你好,可以用正則表達式。比如想要去除id為test的div標簽及其內容:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Person{
public static void main(String[] args) {
//正則表達專式
Pattern p = Pattern.compile("<div.*id='test'.*</div>");
//測試用的html代碼
String str = "<html><body>aa<div id='test'>bb</div></body></html>";
Matcher m = p.matcher(str);
//去除標簽屬
String result = m.replaceAll("");
System.out.println(result);
}
}
㈤ 在java中如何用正則表達式屏蔽javascript腳本
你需要把用戶評論的內容中的:
"&" 替換成 "&"
"<" 替換成 "<"
">" 替換成 ">"
'"' (雙引號回)替換成 '"'
"'" (單引號)替換成 '''
這樣就可以避免答客戶端的危險輸入了
形如<script type="text/javascript">alert("asdf");</script>
的評論就會被直接顯示出來(如同你看到的一樣=。=)
而不會被當作html標簽轉義
====修改====
我的輸入被轉義了,修改下,記得去掉空格
"&" 替換成 "& amp;"
"<" 替換成 "& lt;"
">" 替換成 "& gt;"
'"' (雙引號)替換成 '& quot;'
"'" (單引號)替換成 '& #39;'
㈥ java中如何過濾html的代碼
把需要寫入資料庫的字元通過下面的方法過濾然後內再寫入 public static String converthtml(String input) { if (input == null ||容 input.length() == 0) { return input; } StringBuffer buf = new StringBuffer(input.length() + 6); char ch = ' '; for (int i = 0; i < input.length(); i++) { ch = input.charAt(i); if (ch == '&') { buf.append("&"); } else if (ch == '<') { buf.append("<"); } else if (ch == '>') { buf.append(">"); } else if (ch == ' ') { buf.append(""); } else { buf.append(ch); } } return buf.toString(); }
希望採納
㈦ java如何去掉字元串中的 html標簽
1.去除單個HTML標記
String s="asdfasd<script>asdfsfd</script>1234";
System.out.println(s.replaceAll("<script.*?(?<=/script>)",""));
2.去除所有HTML標記
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HTMLSpirit{ ITjob 遠標教育
public static String delHTMLTag(String htmlStr){
String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定義script的正則表達式
String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定義style的正則表達式
String regEx_html="<[^>]+>"; //定義HTML標簽的正則表達式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //過濾script標簽
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //過濾style標簽
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //過濾html標簽
return htmlStr.trim(); //返迴文本字元串
}
}
㈧ java正則表達式過濾html p標簽
用JavaScript方法如下,JAVA語言類似:
'你的HTML文本'.replace(/.+>(.+)<.+/,'$1')
㈨ 用HTMLParser過濾掉html中所有標簽,留下標題正文等內容,java
現在的網頁,取來title容易,要取到整齊的內源容,就麻煩了。既然是爬蟲,又不可能針對每個頁面都寫一遍。所以,你能解決這問題,是高智商、是值錢的。
<title>和</title>可以認為是標題,用字元串的處理方法即
<content>和</content>不是標準的HTML,不能認為之間的文字就是內容 。雖然<body>和</body>是,可之間的內容也太亂了。
㈩ 在Java截取字元串的時候,如何過濾掉html標簽
去除html標簽
function
strip_tags($string,
$replace_with_space
=
true)
{
if
($replace_with_space)
{
return
preg_replace('!<[^>]*?>!',
'
',
$string);
}
else
{
return
strip_tags($string);
}
}
截取字元函數(匹配各種編碼)
function
truncate($string,
$length
=
80,
$etc
=
'...',
$break_words
=
false,
$middle
=
false){
if
($length
==
0)
return
'';
if
(is_callable('mb_strlen'))
{
if
(mb_detect_encoding($string,
'utf-8,
iso-8859-1')
===
'utf-8')
{
//
$string
has
utf-8
encoding
if
(mb_strlen($string)
>
$length)
{
$length
-=
min($length,
mb_strlen($etc));
if
(!$break_words
&&
!$middle)
{
$string
=
preg_replace('/\s+?(\s+)?$/u',
'',
mb_substr($string,
0,
$length
+
1));
}
if
(!$middle)
{
return
mb_substr($string,
0,
$length)
.
$etc;
}
else
{
return
mb_substr($string,
0,
$length
/
2)
.
$etc
.
mb_substr($string,
-
$length
/
2);
}
}
else
{
return
$string;
}
}
}
//
$string
has
no
utf-8
encoding
if
(strlen($string)
>
$length)
{
$length
-=
min($length,
strlen($etc));
if
(!$break_words
&&
!$middle)
{
$string
=
preg_replace('/\s+?(\s+)?$/',
'',
substr($string,
0,
$length
+
1));
}
if
(!$middle)
{
return
substr($string,
0,
$length)
.
$etc;
}
else
{
return
substr($string,
0,
$length
/
2)
.
$etc
.
substr($string,
-
$length
/
2);
}
}
else
{
return
$string;
}
}
綜合就是
$arc=strip_tags($arc);