Archive of UserLand's first discussion group, started October 5, 1998.

Re: JavaScript HTML entity encoding/decoding functions

Author:Samuel Reynolds
Posted:10/8/1999; 12:55:57 PM
Topic:JavaScript HTML entity encoding/decoding functions
Msg #:11874 (In response to 11860)
Prev/Next:11873 / 11875

I put this together a while back (it's from my JavaScript Suite, http://www.spinwardstars.com/frontier/suites/js.html). Maybe it'll do what you need.

//	Find and replace a string in another string, with optional case-sensitivity.
function ReplaceAll( inText, inFindStr, inReplStr, inCaseSensitive ) {
   //	inText is the text in which to do the search;
   //	inFindStr is the string to find;
   //	inReplStr is the string to substitute into inText in place of inFindStr; and
   //	inCaseSensitive is a boolean value (defaults to false).
   
   var searchFrom = 0;
   var offset = 0;
   var outText = "";
   var searchText = "";
   if ( inCaseSensitive == null ) {
      inCaseSensitive = false;
   }
   if ( inCaseSensitive ) {
      searchText = inText.toLowerCase();
      inFindStr = inFindStr.toLowerCase();
   } else {
      searchText = inText;
   }
   offset = searchText.indexOf( inFindStr, searchFrom );
   while ( offset != -1 ) {
      outText += inText.substring( searchFrom, offset );
      outText += inReplStr;
      searchFrom = offset + inFindStr.length;
      offset = searchText.indexOf( inFindStr, searchFrom );
   }
   outText += inText.substring( searchFrom, inText.length );
   
   return ( outText );
};

function QuoteHtml( inText ) { outText = ReplaceAll( inText, "&", "&" ); outText = ReplaceAll( outText, "<", "<" ); outText = ReplaceAll( outText, ">", ">" ); return ( outText ); }


- Sam




This page was archived on 6/13/2001; 4:52:59 PM.

© Copyright 1998-2001 UserLand Software, Inc.