Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 14th, 2005, 06:11 AM
Friend of Wrox
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replace multiple instances of a String


I have been trying to write some smilie code. I have found a function called replace(). Now this works only once within a string and then stops. What I need is to be able to count multiple instances of a string, in this case ":\)", and replace each one with some HTML ("<img src =\"grin.gif\">"). Any ideas anyone?


\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
Old June 14th, 2005, 06:34 AM
Friend of Wrox
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post

Hi interrupt,

You can use a RegExp with replace() & then set it tp apply globally with the g flag, try this:
var myStr = ":\)some:\)other:\)text";
alert(myStr.replace(/:\)/g, "<img src =\"grin.gif\">"));


Old June 14th, 2005, 06:48 AM
Friend of Wrox
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Chris, I did it the long way:

<script language ="javascript">
function addsmile(){

var SmileStr = document.form1.smilebox.value
var thestr = ":\)"
var found = 0;
var thehtml = "<img src =\"grin.gif\">"
var thenewstr

    while (found != -1){
    found = SmileStr.indexOf(thestr, found)

        if (found >-1){
        thenewstr = SmileStr.replace(thestr, thehtml)
        SmileStr = thenewstr


div1.innerHTML = thenewstr

Thanks Anyway


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple String Replace berna_isct XSLT 5 May 20th, 2008 02:47 AM
creating multiple instances programmatically dcher BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 4 December 28th, 2007 02:21 PM
Multiple browser instances Dilip Nagle Classic ASP Basics 1 September 29th, 2005 10:33 PM
multiple instances of same spreadsheet opening Jingels Excel VBA 1 January 31st, 2005 02:03 AM
One form in multiple instances. Sink Access VBA 0 June 7th, 2004 04:25 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.