Results 1 to 4 of 4

Thread: Global replace..

  1. #1
    Join Date
    Jun 2008

    Angry Global replace..

    I want to replace :) with image globally but it didn't work...

    I read about this on the internet...
    Global Search

    In the following example we will perform a global match, and the word Microsoft will be replaced each time it is found:

    <script type="text/javascript">

    var str="Welcome to Microsoft! ";
    str=str + "We are proud to announce that Microsoft has ";
    str=str + "one of the largest Web Developers sites in the world.";

    document.write(str.replace(/Microsoft/g, "W3Schools"));

    And it works with Microsoft but doesn't work with :)

    MY CODE:
    document.write(str.replace(/:)/g, "<img src="images/smiles/smile1.gif" alt=":-)" border="0" />"));
    It returned me error -
    Error: unmatched ) in regular expression
    Source File: ....../Forums/scripts/functions.js
    Line: 37, Column: 2
    Source Code:

    Last edited by QueenZ; 08-09-2008 at 06:16 PM.

  2. #2
    Join Date
    Jun 2008
    Here is the code but it's not global and i don't know how to make it global..

    	var newmsg = document.post.message.value;
    	search = new Array(
    		":hmm: ",
    		/\[url=([\w]+?:\/\/[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/,
    		/\[url\]((www|ftp|)\.[^ \\"\n\r\t<]*?)\[\/url\]/,
    		/\[url=((www|ftp|)\.[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/,
    		/\[url\](http:\/\/[^ \\"\n\r\t<]*?)\[\/url\]/);
    	replace = new Array(
    	  '<img src="images/smiles/smile1.gif" alt=":)" border="0" />',
    	  '<img src="images/smiles/grin.gif" alt=":-D" border="0" />',
    	  '<img src="images/smiles/wink.gif" alt=";)" border="0" />',
    	  '<img src="images/smiles/sad.gif" alt=":-(" border="0" />',
    	  '<img src="images/smiles/weep.gif" alt=";(" border="0" />',
    	  '<img src="images/smiles/laugh.gif" alt=":lol:" border="0" />',
    	  '<img src="images/smiles/rolleyes.gif" alt=":roll:" border="0" />',
    	  '<img src="images/smiles/devil.gif" alt=":devil:" border="0" />',
    	  '<img src="images/smiles/cool2.gif" alt=":cool:" border="0" />',
    	  '<img src="images/smiles/yucky.gif" alt=":yucky:" border="0" />',
    	  '<img src="images/smiles/no.gif" alt=":no:" border="0" />',
    	  '<img src="images/smiles/angry.gif" alt=":angry:" border="0" />',
    	  '<img src="images/smiles/yes.gif" alt=":yes:" border="0" /> ',
    	  '<img src="images/smiles/huh.gif" alt=":huh:" border="0" /> ',
    	  '<img src="images/smiles/hmm.gif" alt=":hmm:" border="0" />',
    	  "<img src=\"$1\">",
    	  "<a href=\"$1\" target=\"blank\">$2</a>",
    	  "<a href=\"http://$1\" target=\"blank\">$1</a>",
    	  "<a href=\"$1\" target=\"blank\">$1</a>",
    	  "<a href=\"mailto:$1\">$1</a>",
    	  "<a href=\"$1\" target=\"blank\">$1</a>");
    	for(i = 0; i < search.length; i++) {
    		newmsg = newmsg.replace(search[i],replace[i]);
    Please help guys!

  3. #3
    Join Date
    Jan 2005
    There are 2 problems with it. The first is that you need to put the second argument inside quotation marks (preferrably single. if you use double then you have to escape all of the doubles inside the argument string). The second thing is that you need to escape (\) the closing parenthesis before /g. Otherwise the browser thinks that you are closing the replace call:

    str.replace(/:\)/g, '<img src="images/smiles/smile1.gif" alt=":-)" border="0" />');

  4. #4
    Join Date
    Aug 2007
    As a general rule of thumb, escape (i.e. add a backslash) in front of all punctuation.
    is not almost guaranteed to have any problems, even though
    does work.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center