Connecting Tech Pros Worldwide Forums | Help | Site Map

Replacing more than one character in a document or string

Newbie
 
Join Date: Feb 2007
Posts: 12
#1: Apr 14 '07
I'm trying to make a layout in which the titles of articles (and possibly articles themselves, although they're written in an editor that breaks lines so I can't as yet include the article variable within a script... if somebody knows a way around that, it'd be fantastic, but I'd settle for the simpler title option) are written in a custom font. The only way I can think of doing this is including a piece of JavaScript that replaces all characters with small GIFs I will preload. The problem is that I don't seem to be able to find a way to replace more than one character of a certain type (e.g. all "p"s, "t"s, "?"s or whatever) in the same string or document.

At the moment I am using the simple string method of:


Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var first="The quick sly fox jumped over the lazy brown dog."
  3. document.write(first.replace(/o/gi, "<img src=myimage.GIF>"))
  4. </script> 

I'm at a loss as to how to adapt this script to replace more than one type of character with more than one type of image. More than likely I need to scrap this one and start from scratch, but I'm not really good enough with JavaScript to write my own.

Help would be much appreciated!

Thanks,

Hannah

Familiar Sight
 
Join Date: Feb 2007
Posts: 207
#2: Apr 14 '07

re: Replacing more than one character in a document or string


Quote:

Originally Posted by Schannah

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var first="The quick sly fox jumped over the lazy brown dog."
  3. document.write(first.replace(/o/gi, "<img src=myimage.GIF>"))
  4. </script> 

Let's say your images are named with the scheme myimage[character].gif
Expand|Select|Wrap|Line Numbers
  1. var first="The quick sly fox jumped over the lazy brown dog."
  2. document.write(first.replace(/(\w)/gi, "<img src=myimage$1.GIF>"))
  3.  
This will work for alphanumeric characters. You could have cascaded replace() calls to specify other types of characters in different ways.
Reply