0%

Js 从字符串html中获取文本

需求

富文本编辑器包含 html 标签,期望去掉 html 标签只保留文本

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function extractContent(s) {
var span = document.createElement('span');
span.innerHTML = s;
return span.textContent || span.innerText;
};
const str = `<!DOCTYPE HTML>
<html>
<head>
<title>#Persistent</title>
<meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=iso-8859-1\\">
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"IE=edge\\">
<link href=\\"../static/theme.css\\" rel=\\"stylesheet\\" type=\\"text/css\\"/>
<script src=\\"../static/content.js\\" type=\\"text/javascript\\"></script>
</head>
<body>
</html>
`;
extractContent(str)

测试样本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE HTML>
<html>
<head>
<title>#Persistent</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
<link href=\"../static/theme.css\" rel=\"stylesheet\" type=\"text/css\"/>
<script src=\"../static/content.js\" type=\"text/javascript\"></script>
</head>
<body>
<br />
<h1>#Persistent</h1>
<p>Keeps a script permanently running (that is, until the user closes it or <a href=\"ExitApp.htm\">ExitApp</a> is encountered).</p>
<pre class=\"Syntax\">#Persistent</pre>
<p>If this directive is present anywhere in the script, that script will stay running after the auto-execute section (top part of the script) completes. This is useful in cases where a script contains <a href=\"SetTimer.htm\">timers</a> and/or <a href=\"Menu.htm\">custom menu items</a> but not <a href=\"../Hotkeys.htm\">hotkeys</a>, <a href=\"../Hotstrings.htm\">hotstrings</a>, or any use of <a href=\"OnMessage.htm\">OnMessage()</a> or <a href=\"Gui.htm\">Gui</a>.</p>
<p>If this directive is added to an existing script, you might want to change some or all occurrences of <a href=\"Exit.htm\">Exit</a> to be <a href=\"ExitApp.htm\">ExitApp</a>. This is because <a href=\"Exit.htm\">Exit</a> will not terminate a persistent script; it terminates only the <a href=\"../misc/Threads.htm\">current thread</a>.</p>
<p>In v1.0.16+, this directive also makes a script single-instance. To override this or change the way single-instance behaves, see <a href=\"_SingleInstance.htm\">#SingleInstance</a>.</p>
<h3>Related</h3>
<p><a href=\"_SingleInstance.htm\">#SingleInstance</a>, <a href=\"SetTimer.htm\">SetTimer</a>, <a href=\"Menu.htm\">Menu</a>, <a href=\"Exit.htm\">Exit</a>, <a href=\"ExitApp.htm\">ExitApp</a></p>
<h3>Example</h3>
<pre class=\"NoIndent\">#Persistent</pre>
</body>
</html>