Solved

Using Replace Function

Posted on 2004-10-08
4
197 Views
Last Modified: 2010-04-17
How to use replace function in java script and on what situation should we use it
    thanks
0
Comment
Question by:sanilkrishna
  • 2
4 Comments
 
LVL 1

Expert Comment

by:alfanhendro
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.
0
 
LVL 11

Accepted Solution

by:
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";
a=a.replace(/t[es]{2}t/gi,"cool");
alert(a);

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+$/,'');}

Enjoy!
Pratap
0
 

Author Comment

by:sanilkrishna
ID: 12273766
Hello pratap,

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

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

Question
-----------
  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,
   sanil
0
 
LVL 11

Expert Comment

by:pratap_r
ID: 12274173
its the standard regex syntax, you can check http://www.regular-expressions.com for detailed explanations..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 57
array11 challenge 16 60
mapBully challenge 6 114
Counting documents in a Domino View 3 64
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is about my first experience with programming Arduino.
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 …
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now