Strip all tags keeping their text content:
var content = "<a href=\"1\">I</a> was going here and then <a href=\"that\">that</a> happened.";
// parse the HTML string into DOM
var container = document.createElement('div');
container.innerHTML = content;
// retrieve the textContent, or innerText when textContent is not available
var clean = container.textContent || container.innerText;
console.log(clean); //"I was going here and then that happened."
Fiddle
As per OP's comment, the text only contains anchor tags, so this method should work fine.
You may drop the || container.innerText
if you don't need IE <= 8 support.
Reference
textContent
- Gets or sets the text content of a node and its descendents.
innerText
- Sets or retrieves the text between the start and end tags of the object.
Just to answer the question in the title, here is a way to remove only the anchor elements:
var content = "<a href=\"1\">I</a> was going here and then <a href=\"that\">that</a> happened.";
var container = document.createElement('div');
container.innerHTML = content;
var anchors = container.getElementsByTagName('a'),
anchor;
while (anchor = anchors[0]) {
var anchorParent = anchor.parentNode;
while (anchor.firstChild) {
anchorParent.insertBefore(anchor.firstChild, anchor);
}
anchorParent.removeChild(anchor);
}
var clean = container.innerHTML;
console.log(clean); //"I was going here and then that happened."
Fiddle
Reference
Node.insertBefore
- Inserts the specified node before a reference element as a child of the current node.
Node.removeChild
- Removes a child node from the DOM.
Element.getElementsByTagName
- Returns a list of elements with the given tag name. The subtree underneath the specified element is searched, excluding the element itself.
Even though OP is not using jQuery, here is a practically equivalent jQuery version of the above for whom it may concern:
var content = "<a href=\"1\">I</a> was going here and then <a href=\"that\">that</a> happened.";
var clean = $('<div>').append(content).find('a').contents().unwrap().end().end().html();
console.log(clean); //"I was going here and then that happened."
Fiddle
NOTE
All of the solutions in this answer assume that the content
is valid HTML -- it won't handle malformed markup, unclosed tags, etc. It also considers that the markup is safe (XSS-sanitized).
If the criteria above is not met, you're better off using a regex solution. Regex should usually be your last resort when the use case involves parsing HTML as it is very easy to break when tested against arbitrary markup (related: virgin-devouring ponies), but your use case seems very simple and a Regex solution may be just what you need.
This answer provides non-regex solutions so that you may use these once (if ever) a regex solution breaks.