dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
Pritom Nandy
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » TypeScript » Scripting Languages » Javascript

Javascript string replace all


Posted Date:     Category: Javascript    
Author: Member Level: Gold    Points: 13



 


JavaScript String Replace All

Today When I was working I came across the need to replace all the “’” marks with null or with nothing like “” as the “’” mark produces an error while handling with database I have the StringBuldier Class and could replace all at once but to use that it will be a server side code that means irrespective of anything on textchanged event the page should be postback so I decided to use the JavaScript function replace ()

Now the problem with this JavaScript String Replace function is that it replaces the first occurrence in the string. It takes two simple parameters. The first parameter is the pattern to find and the second parameter is the string to replace the pattern with when found. The JavaScript function does not Replace All...



str="http://www.msdotnetheaven.com and http://www.msdotnetheaven.com";

str = str.replace(/heaven/,"mentor")




The output :

 
str=”http://www.msdotnetmentor.com and http://www.msdotnetheaven.com”;



To make the replace function of the JavaScript to replace all the occurrences of the matched string we have to apply a trick and the trick is, use the g modifier like this:



str=”http://www.msdotnetheaven.com and http://www.msdotnetheaven.com”;

str = str.replace(/heaven/g,”mentor”)



The output:


str=”http://www.msdotnetmentor.com and http://www.msdotnetmentor.com”;





Did you like this resource? Share it with your friends and show your love!


Responses to "Javascript string replace all"
Author: Sathish KUmar.L    18 Mar 2009Member Level: Silver   Points : 0
Superb Trick


Author: Miss Meetu Choudhary    18 Mar 2009Member Level: Gold   Points : 1
Thanks Sathish...

I faced this problem and then finally got the solution so thought to share with you all too



Author: Pradeep Iyer    19 Mar 2009Member Level: Gold   Points : 1
Hi Meetu,

That's is good post.

Very useful for the developers.

Keep posting more and more......

Regards,
Pradeep Y.



Author: Bunty    20 Mar 2009Member Level: Gold   Points : 2
Hi,

Very nice piece of code meetuji.

Keep contributing like this.

Regards
S.S.Bajoria



Author: Deepika Haridas    20 Mar 2009Member Level: Gold   Points : 1
Hi,

Great post..
Must say Nice trick..!!

Thanks for posting it...

regards,
Deepika



Author: R.Jaya kumar (JK)    20 Jun 2009Member Level: Gold   Points : 1
Dear Mam
Really Excellent But what is meaning g

any reserve word

thanks
jayakumar



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: No scrollbar on HTML web pages - JavaScript code
    Previous Resource: Validation for Email ID
    Return to Resources
    Post New Resource
    Category: Javascript


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Javascript  .  Replace all  .  Replace()  .  Replaceall  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.