Bad word filter : Filter « Regular Expressions « JavaScript Tutorial

    <title>Bad Words Example</title>
    <script type="text/javascript">

            function filterText(sText) {
                var reBadWords = /badword|anotherbadword/gi;
                return sText.replace(reBadWords, "****");

            function showText() {
                var oInput1 = document.getElementById("txt1");
                var oInput2 = document.getElementById("txt2");
                oInput2.value = filterText(oInput1.value);
    <textarea rows="10" cols="50" id="txt1">badword anotherbadword</textarea><br />
    <input type="button" value="Filter Bad Words" onclick="showText()" /></p>
    <P>Filtered Text:<br />
    <textarea rows="10" cols="50" id="txt2"></textarea></p>


26.5.1.Trim the leading and trailing spaces
26.5.2.Strip HTML
26.5.3.Bad word filter
26.5.4.Use regular expression to remove bad words