最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove HTML tags in script - Stack Overflow

programmeradmin4浏览0评论

I've found this piece of code on the internet. It takes a sentence and makes every single word into link with this word. But it has weak side: if a sentence has HTML in it, this script doesn't remove it.

For example: it replaces '<b>asserted</b>' with '/<b>asserted</b>'

Could you please tell me what to change in this code for it to change '<b>asserted</b>' to ''.

var content = document.getElementById("sentence").innerHTML;

var punctuationless = content.replace(/[.,\/#!$%\؟^?&\*;:{}=\-_`~()”“"]/g, "");
var mixedCase = punctuationless.replace(/\s{2,}/g);
var finalString = mixedCase.toLowerCase();

var words = (finalString).split(" ");

var punctuatedWords = (content).split(" ");

var processed = "";
for (i = 0; i < words.length; i++) {
    processed += "<a href = \"/" + words[i] + "\">";
    processed += punctuatedWords[i];
    processed += "</a> ";
}

document.getElementById("sentence").innerHTML = processed;

I've found this piece of code on the internet. It takes a sentence and makes every single word into link with this word. But it has weak side: if a sentence has HTML in it, this script doesn't remove it.

For example: it replaces '<b>asserted</b>' with 'http://www.merriam-webster./dictionary/<b>asserted</b>'

Could you please tell me what to change in this code for it to change '<b>asserted</b>' to 'http://www.merriam-webster./dictionary/asserted'.

var content = document.getElementById("sentence").innerHTML;

var punctuationless = content.replace(/[.,\/#!$%\؟^?&\*;:{}=\-_`~()”“"]/g, "");
var mixedCase = punctuationless.replace(/\s{2,}/g);
var finalString = mixedCase.toLowerCase();

var words = (finalString).split(" ");

var punctuatedWords = (content).split(" ");

var processed = "";
for (i = 0; i < words.length; i++) {
    processed += "<a href = \"http://www.merriam-webster./dictionary/" + words[i] + "\">";
    processed += punctuatedWords[i];
    processed += "</a> ";
}

document.getElementById("sentence").innerHTML = processed;
Share edited Oct 18, 2016 at 9:56 Al.G. 4,4266 gold badges34 silver badges62 bronze badges asked Oct 18, 2016 at 9:33 Максим ЦіпанМаксим Ціпан 231 silver badge3 bronze badges 3
  • 1 Possible duplicate of JavaScript: How to strip HTML tags from string? – evolutionxbox Commented Oct 18, 2016 at 9:34
  • You might want to escape the . in your regexp like \., like you do with \- for instance. – Azamantes Commented Oct 18, 2016 at 9:37
  • 2 Possible duplicate of Strip HTML from Text JavaScript – Andreas Commented Oct 18, 2016 at 9:41
Add a ment  | 

3 Answers 3

Reset to default 5

This regex /<{1}[^<>]{1,}>{1}/g should replace any text in a string that is between two of these <> and the brackets themselves with a white space. This

  var str = "<hi>How are you<hi><table><tr>I<tr><table>love cake<g>"
  str = str.replace(/<{1}[^<>]{1,}>{1}/g," ")
  document.writeln(str);

will give back " How are you I love cake".

If you paste this

var stripHTML = str.mixedCase(/<{1}[^<>]{1,}>{1}/g,"")

just below this

var mixedCase = punctuationless.replace(/\s{2,}/g);

and replace mixedCase with stripHTML in the line after, it will probably work

function stripAllHtml(str) {
  if (!str || !str.length) return ''

  str = str.replace(/<script.*?>.*?<\/script>/igm, '')

  let tmp = document.createElement("DIV");
  tmp.innerHTML = str;

  return tmp.textContent || tmp.innerText || "";
}

stripAllHtml('<a>test</a>')

This function will strip all the HTML and return only text.

Hopefully, this will work for you

if you need to remove HTML tags And HTML Entities You can use

const text = '<p>test content </p><p><strong>test bold</strong>&nbsp;</p>'
text.replace(/<[^>]*(>|$)|&nbsp;|&zwnj;|&raquo;|&laquo;|&gt;/g, '');

the result will be "test content test bold"

发布评论

评论列表(0)

  1. 暂无评论