Remove Specific HTML Tag with Its Content from JavaScript String

Remove specific HTML tag with its content from javascript string

You should avoid parsing HTML using regex. Here is a way of removing all the <a> tags using DOM:

// your HTML textvar myString = '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>';myString += '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>'myString += '<table><tr><td>Some text ...<a href="#">label...</a></td></tr></table>'
// create a new dov containervar div = document.createElement('div');
// assing your HTML to div's innerHTMLdiv.innerHTML = myString;
// get all <a> elements from divvar elements = div.getElementsByTagName('a');
// remove all <a> elementswhile (elements[0]) elements[0].parentNode.removeChild(elements[0])
// get div's innerHTML into a new variablevar repl = div.innerHTML;
// display itconsole.log(repl)
/*<table><tbody><tr><td>Some text ...</td></tr></tbody></table><table><tbody><tr><td>Some text ...</td></tr></tbody></table><table><tbody><tr><td>Some text ...</td></tr></tbody></table>*/

Remove specific tags and their content in javascript

Assuming that the div is an actual string, try this regex instead:

<div.*<\/div>
  • A dot is any one single character: .
  • followed by an asterisk witch is zero or more instances of whatever precedes it: * *
  • and in order to interpret the forward slash literally escape it with a backward slash: \

* a plus: + would suffice as well -- one or more instances of whatever precedes it.

var res, html = document.getElementById('source').value;res = html.replace(/<div.*<\/div>/g, "");document.getElementById('result').innerHTML = res;
textarea {  width: 100%;  height: 120px;  padding: 5px;  border: 1px solid rgba(0, 0, 0, .3);}
<textarea id="source">  <p>      <input type="text"/>      <div class="parent"><div>inner01</div><div>inner02</div></div>      <span></span>      <div class="parent"><input></div>  </p></textarea><textarea id="result"></textarea>

How to remove HTML tag (not a specific tag ) with content from a string in javascript

Removing all HTML tags and the innerText can be done with the following snippet. The Regexp captures the opening tag's name, then matches all content between the opening and closing tags, then uses the captured tag name to match the closing tag.

const regexForStripHTML = /<([^</> ]+)[^<>]*?>[^<>]*?<\/\1> */gi;
const text = "OCEP <sup>®</sup> water product";
const stripContent = text.replaceAll(regexForStripHTML, '');
console.log(text);
console.log(stripContent);

Javascript: remove all instances of certain HTML tags in a non-html string

You can avoid this particular issue by using a lazy match in your regex instead of a greedy one. Try this:

var deleteTag = '<custom-tag>.*?<\/custom-tag>';
string= string.replace(new RegExp(deleteTag , 'g'), '');

If you have nested <custom-tag>, though, regex is probably not the tool for the job.

How to remove html tags from an Html string using RegEx?

You can use

.replace(/<br>(?=(?:\s*<[^>]*>)*$)|(<br>)|<[^>]*>/gi, (x,y) => y ? ' & ' : '')

See the JavaScript demo:

const text = '<div class="ExternalClassBE95E28C1751447DB985774141C7FE9C"><p>Tina Schmelz<br></p><p>Sascha Balke<br></p></div>';
const regex = /<br>(?=(?:\s*<[^>]*>)*$)|(<br>)|<[^>]*>/gi;
console.log(
text.replace(regex, (x,y) => y ? ' & ' : '')
);

Remove HTML content groups from start to end of string in JavaScript

This is pure regex solution:

var str = "Hello <script> console.log('script tag') </script> World";
var repl = str.replace(/<([^.]+)>.*?<\/\1>/ig, '');
//=> "Hello World"

with an assumption that there is no < OR > between opening and closing tags.

How to remove specific html tag and it's content from string in php

One reliable way to do that, is to use a XML parser and its methods, e.g. SimpleXML. The advantage over string replacing with fixed string lengths (strpos(…) + 5) or using regular expressions, is that you really can find all occurrences of a specified element, even if it bears attributes.

<?php

$score = '7-6<sup>6</sup>, 7-6<sup>5</sup>, 6<sup>10</sup>-7, 6-0, 6-2';

/* Add a temporary wrapper element, so that any string input may be parsed as valid XML */
$xml = new SimpleXMLElement(sprintf('<html>%s</html>', $score));

/* Find the elements to be deleted using XPATH */
foreach ($xml->xpath('sup') as $sup) {
/* Remove this node using unset() */
unset($sup[0]);
}

/* Remove the temporary wrapper and the XML header, which is printed by asXML() */
$xmlOutput = trim(str_replace(['<?xml version="1.0"?>', '<html>', '</html>'], '', $xml->asXML()));

var_dump($xmlOutput);

See also https://en.wikipedia.org/wiki/XPath

Another option would be to use strip_tags() and list all allowed tags in the second attribute.

Given a string, how can I use JavaScript to remove all 'HTML tags' except a specific 'tag' (and its 'children')?

It looks rather awful, but it woks (with some limitations):

  • split the string by <math> and </math>
  • remove all html tags in every second element
  • add <math> and </math> around every second element
  • join the array back into a string

const html =
'<p><span>Initial data: <math><msqrt><mo>y</mo></msqrt></math></span> <div><strong>hello world</strong><math><msqrt><mo>x</mo></msqrt></math></div></p>'

var text = html.split('<math>')
.map(t => t.split('</math>')).flat()
.map((t, i) => {return (i % 2==0 ) ? t.replace(/<.+?>/g,''): t })
.map((t, i) => {return (i % 2==0 ) ? t : '<math>' + t + '</math>' })
.join('');

console.log(text);

// OUTPUT: Initial data: <math><msqrt><mo>y</mo></msqrt></math> hello world<math><msqrt><mo>x</mo></msqrt></math>


Related Topics



Leave a reply



Submit