Regular Expression Worksheet

[Total: 1    Average: 4/5]

This function will replace any item surrounded by spaces, tabs, returns, ., ?,

Head Code:

<script language="JavaScript"><!--
        // Andy Smith smitha40@msu.edu 2004 JavaScript Word Replace function
        // type should be 'nocase' for searches that are not case sensitive and anything else for case sensitive
        // this function matches words surrounded by spaces, tabs, return characters, !, ? or .
        function wordreplace(words, replacestr, replacewith, type ) 
        {
                if (replacestr != ' ')
                {
                        var temp = ' ' + words + ' ';
                        var toreturn = ' ' + words + ' ';
                }
                else
                {
                        var temp = '*' + words + '*';
                        var toreturn = '*' + words + '*';
                }
                
                var tempreplacestr = replacestr;
                var tempreplacewith = replacewith;
                if ( type == 'nocase')
                        {
                                temp = temp.toUpperCase();
                                tempreplacestr = tempreplacestr.toUpperCase();
                                tempreplacewith = tempreplacewith.toUpperCase();
                        }
                var subtemppre;
                var subtemppost;
                var index;
                var Tindex;
                
                if (tempreplacestr != tempreplacewith)
                {
                        
                        while( temp.indexOf(tempreplacestr) != -1)
                        {
                                index = temp.indexOf(tempreplacestr);
                                Tindex = index + (toreturn.length - temp.length);
                                
                                
                                
                                if ( (tempreplacestr != ' ') && (tempreplacestr != 'n') && (tempreplacestr != 't')  && (tempreplacestr != '.') && (tempreplacestr != '?') && (tempreplacestr != '!') )
                                {
                                        
                                        subtemppre = temp.substring(index-2, temp.length);
                                        subtemppretab = temp.substring(index-1, temp.length);
                                        subtemppost = temp.substring(index, temp.length);
                                        
                                        if(  
                                        ( (temp.charAt(index-1) == ' ') || (subtemppre.indexOf('n') == 1) || (subtemppretab.indexOf('t') == 0) ) && 
                                        ( (subtemppost.indexOf('n') == tempreplacestr.length+1 ) || (temp.charAt(index+tempreplacestr.length) == ' ') || (subtemppost.indexOf('t') == tempreplacestr.length ) || (temp.charAt(index+tempreplacestr.length) == '.') || (temp.charAt(index+tempreplacestr.length) == '?') || (temp.charAt(index+tempreplacestr.length) == '!')) )
                                        {
                                                toreturn = toreturn.substring(0, Tindex) + replacewith + toreturn.substring(Tindex+tempreplacestr.length, toreturn.length);
                                        }
                                                
                                        temp = temp.substring(index+tempreplacestr.length, temp.length);
                                        
                                }
                                else
                                {
                                        
                                                toreturn = toreturn.substring(0, Tindex) + replacewith + toreturn.substring(Tindex+tempreplacestr.length, toreturn.length);
                                                temp = temp.substring(index+tempreplacestr.length, temp.length);
                                                
                                        
                                }
                                
                                }
                                
                                
                        }
                
                        return toreturn.substring(1, toreturn.length-1);
        }
//--></script>

Body Code:

<form action="test.html" method="post" name="form">
Replace:<input name="rstring" type="text"> With: <input name="rwstring" type="text"> Option:<select name="Type">
  <option value="case">case specific</option>
  <option value="nocase">not case specific</option>
</select><br>
<textarea name="code" cols="40" rows="10"></textarea>
<input name="convert" type="button" value="click" onClick="document.form.code.value = wordreplace(document.form.code.value, document.form.rstring.value, document.form.rwstring.value, document.form.Type[document.form.Type.selectedIndex].value);">
</form>

Times Viewed: 9

Leave a Reply

Your email address will not be published. Required fields are marked *