jQuery: Using Regular Expressions to Remove HTML Tags from Content | Quisitive
jQuery: Using Regular Expressions to Remove HTML Tags from Content
August 27, 2013
Quisitive
The following is a very simple example of using jQuery to remove HTML Tags from your content:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Strip HTML</title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>

<script type="text/javascript">

$(document).ready(function () {

var htmlString = $("#textbox1").val();

/* replace escaped brackets with real ones */

var htmlString2 = htmlString.replace(/&(lt|gt);/g, function (strMatch, p1) {

return (p1 == "lt") ? "<" : ">";

});

$("#textbox2").val(htmlString2);

/* strip text */

var htmlString3 = htmlString2.replace(/<\/?[^>]+(>|$)/g, "");

$("#textbox3").val(htmlString3);

});

</script>

</head>

<body>

    <input type="text" id="textbox1" name="textbox1" style="width:350px;height:40px;" value="<br><strong>This is a test</strong><p>"/><br />

    <input type="text" id="textbox2" name="textbox2" style="width:350px;height:40px;" value=""/><br />

    <input type="text" id="textbox3" name="textbox3" style="width:350px;height:40px;" value=""/>

</body>

</html>

And here is the result: