Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 14th, 2005, 06:11 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default Replace multiple instances of a String

Hi,

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?

Thanks
interrupt

__________________
\'sync\' &lt;cr&gt;
The name specified is not recognized as an internal or external command, operable program or batch file.
Reply With Quote
  #2 (permalink)  
Old June 14th, 2005, 06:34 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: , , United Kingdom.
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi interrupt,

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

Chris

Reply With Quote
  #3 (permalink)  
Old June 14th, 2005, 06:48 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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)
        alert(found)
        found++
        }
        found-1
        SmileStr = thenewstr

    }

div1.innerHTML = thenewstr
}
</script>

Thanks Anyway

interrupt

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:16 PM.


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