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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

732 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