Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Intermediate
Dec 15, 2001

Replace Function in Javascript


JavaScript lacks a Replace() function. This functionality can only be achieved in Javascript using RegularExpression, though not all browsers support it. The code below is for replace and can work on all browers.
 
function Replace(StrSource,StrFind,StrReplace,_
IsCaseSensitive)
	{
		var StrReplacedContent="";
		var FindPos;
		var StrOriginal=new String(StrSource)_
.toString();
		var StrOriginalSource=new String(StrSource);
		var FindPos=StrOriginalSource.toString()_
.indexOf(StrFind);
		while(FindPos>-1)
			{

				StrReplacedContent+=_
StrOriginal.substring(0,FindPos) + StrReplace;
				StrOriginalSource=_
StrOriginalSource.substring(FindPos+StrFind.length);
				StrOriginal=_
StrOriginal.substring(FindPos+StrFind.length);
				FindPos=StrOriginalSource_
.indexOf(StrFind);
			}
		StrReplacedContent+=StrOriginalSource;
		return StrReplacedContent;
	}
Prasad Haridass
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date