www.webdeveloper.com
Results 1 to 4 of 4

Thread: Global replace..

  1. #1
    Join Date
    Jun 2008
    Posts
    27

    Angry Global replace..

    Hi,
    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"));

    </script>
    And it works with Microsoft but doesn't work with :)

    MY CODE:
    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:
    /:)/g,


    PLEASE HELP!!
    Last edited by QueenZ; 08-09-2008 at 05:16 PM.

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

    Code:
    	var newmsg = document.post.message.value;
    	search = new Array(
    		":)",
    		":D",
    		";)",
    		":(",
    		";(",
    		":lol:",
    		":roll:",
    		":devil:",
    		":cool:",
    		":yucky:",
    		":no:",
    		":angry:",
    		":yes:",
    		":huh:",
    		":hmm: ",
    		/\[img\](.*?)\[\/img\]/,
    		/\[url=([\w]+?:\/\/[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/,
    		/\[url\]((www|ftp|)\.[^ \\"\n\r\t<]*?)\[\/url\]/,
    		/\[url=((www|ftp|)\.[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/,
    		/\[email\](([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+))\[\/email\]/,
    		/\[b\](.*?)\[\/b\]/,
    		/\[i\](.*?)\[\/i\]/,
    		/\[u\](.*?)\[\/u\]/,
    		/\[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>",
    	  "<b>$1</b>",
    	  "<i>$1</i>",
    	  "<u>$1</u>",
    	  "<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
    Posts
    3,067
    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:

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

  4. #4
    Join Date
    Aug 2007
    Posts
    3,767
    As a general rule of thumb, escape (i.e. add a backslash) in front of all punctuation.
    Code:
    /\:\)/g
    is not almost guaranteed to have any problems, even though
    Code:
    /:\)/g
    does work.
    Great wit and madness are near allied, and fine a line their bounds divide.

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



Recent Articles