Basically I just need the effect of copying that HTML from browser window and pasting it in a textarea element.
For example I want this:
<p>Some</p>
<div>text<br />Some</div>
<div>text</div>
to become this:
Some
text
Some
text
Basically I just need the effect of copying that HTML from browser window and pasting it in a textarea element.
For example I want this:
<p>Some</p>
<div>text<br />Some</div>
<div>text</div>
to become this:
Some
text
Some
text
This might answer your question: http://stackoverflow.com/questions/822452/strip-html-from-text-javascript
Three steps.
First get the html as a string.
Second, replace all <BR /> and <BR> with \r\n.
Third, use the regular expression "<(.|\n)*?>" to replace all markup with "".
I tried to find some code I wrote for this a while back that I used. It worked nicely. Let me outline what it did, and hopefully you could duplicate its behavior.
You could even expand this more to format things like ordered and unordered lists. It really just depends on how far you'll want to go.
EDIT
Found the code!
public static string Convert(string template)
{
template = Regex.Replace(template, "<img .*?alt=[\"']?([^\"']*)[\"']?.*?/?>", "$1"); /* Use image alt text. */
template = Regex.Replace(template, "<a .*?href=[\"']?([^\"']*)[\"']?.*?>(.*)</a>", "$2 [$1]"); /* Convert links to something useful */
template = Regex.Replace(template, "<(/p|/div|/h\\d|br)\\w?/?>", "\n"); /* Let's try to keep vertical whitespace intact. */
template = Regex.Replace(template, "<[A-Za-z/][^<>]*>", ""); /* Remove the rest of the tags. */
return template;
}
If that HTML is visible within your web page, you could do it with the user selection (or just a TextRange
in IE). This does preserve line breaks, if not necessarily leading and trailing white space:
<div id="container">
<p>Some</p>
<div>text<br />Some</div>
<div>text</div>
</div>
<script type="text/javascript">
function getInnerText(el) {
var sel, range, innerText = "";
if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
sel = window.getSelection();
sel.selectAllChildren(el);
innerText = "" + sel;
sel.removeAllRanges();
} else if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
range = document.body.createTextRange();
range.moveToElementText(el);
innerText = range.text;
}
return innerText;
}
var el = document.getElementById("container");
alert(getInnerText(el));
</script>