如何使用JSOUP删除特定元素内的换行符

时间:2018-03-15 08:03:57

标签: jsoup

<script type="text/javascript">
function answer1()
{
alert("This answer is incorrect. " +
"The value returned by SQL%ROWCOUNT reflects the most recently "+ 
" executed implicit cursor. Because a row was inserted by the procedure, this attribute returns 1. " );
}

function answer2()
{
alert("This answer is incorrect. " +
"The value returned by SQL%ROWCOUNT reflects the most recently executed implicit cursor. In this case, it should be fairly obvious: I"+ 
" insert a row and then call SQL%ROWCOUNT. “Last Count=1” is displayed. " );
}

function answer3()
{
alert("This answer is correct. " +
"The update changes no rows, because the table is currently empty. The insert procedure does not" +
" perform an insert. So the call to SQL%ROWCOUNT reflects the impact of the update: 0 rows updated. " );
}

function answer4()
{
alert("This answer is correct. " +
"No rows are modified, because the WHERE clause specifies ID = 0. But no error is raised, so the "+ 
"next statement is executed and SQL%ROWCOUNT is set to 0. " );
}
</script>

当我正在进行html配对时,我会使用标记代替下面的html中的换行符

"<script type="text/javascript"><br/>function answer1()<br/>{<br/>alert("This answer is incorrect. " +<br/>"The value returned by SQL%ROWCOUNT reflects the most recently "+ <br/>" executed implicit cursor. Because a row was inserted by the procedure, this attribute returns 1. " );<br/>}<br/>function answer2()<br/>{<br/>alert("This answer is incorrect. " +<br/>"The value returned by SQL%ROWCOUNT reflects the most recently executed implicit cursor. In this case, it should be fairly obvious: I"+ <br/>" insert a row and then call SQL%ROWCOUNT. “Last Count=1” is displayed. " );<br/>}<br/></script>"

我只是想用jsoup删除这个br标签。我可以就上述问题得到一些建议。

1 个答案:

答案 0 :(得分:0)

您可以使用Jsoup获取脚本标记的外部html,然后使用String替换手动删除<br/>标记。

Element scriptElement = document.select("script").first();

String scriptHtml = scriptElement.outerHtml();

String htmlWithoutBr = scriptHtml.replaceAll("<br/>", "\n");
// or if you don't need to preserve the line breaks use below
// String htmlWithoutBr = scriptHtml.replaceAll("<br/>", " ");
System.out.println(htmlWithoutBr);