Using Replace Function

Posted on 2004-10-08
Last Modified: 2010-04-17
How to use replace function in java script and on what situation should we use it
Question by:sanilkrishna
  • 2

Expert Comment

ID: 12256889
replace is used to replace some part of a string with another substring.
example code:

var strText = new String() ;
// Prepare a string of chars.
strText = 'This is XYZ a text. ';
// Write it into our document.
document.write ( strText ) ;
// Replace the XYZ with nothing.
var newstr = strText.replace('XYZ', '') ;
// Write it into our document.
document.write ( newstr ) ;

will display :

This is XYZ a text. This is  a text.
LVL 11

Accepted Solution

pratap_r earned 50 total points
ID: 12257103
There is another use! you could use RegEx for searching and replacing. Heres an eg..

a="this is a test message. this is a tset message";

note that there is a spelling mistake in one of the 'test' words, but the regex replaces both instances correctly!

heres another...
function trim(str){return str.replace(/^\s+/,'').replace(/\s+$/,'');}


Author Comment

ID: 12273766
Hello pratap,

I find this solution more appropriate as my scenario invites to change the dvision tag as given below

 where beneficiaryDiv is the division tag as given below
<div id="beneficiaryDiv" style="display:none">
 var temp = paneBeneficiary.replace(/transactionDetails\[\d+\]/g, "transactionDetails["+transCtr++ +"]");

  the point which is confounding is what does the delimiters play a role here in the Regex function?
 could you please enlighten what is the exact format of the regex function which you have given me

  thanks and regards,
LVL 11

Expert Comment

ID: 12274173
its the standard regex syntax, you can check for detailed explanations..

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Batch file output 20 78
Specific format 21 144
creating threads in delphi 1 55
Tembedded WB animatid gifs not animated on some pcs 2 34
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now