Connecting Tech Pros Worldwide Help | Site Map

confusion in search and replace..

Newbie
 
Join Date: Jan 2007
Posts: 2
#1: Jan 25 '07
Hi..
I having trouble in search and replace a specific string

;) -- replace with "wink"
;;) --reblace with "DBL_wink"

When i give in my Js file

mytext = mytext.replace(/\;\)/ig,"wink")
mytext = mytext.replace(/\;;\)/ig,"DBL_wink")

The outoput is

;) -- is REPLACED with "wink" ---correct
;;)-- is REPLACED with " ;wink" --not correct (must be "DBL_wink")

Waht am i missing here
Can anyone help me please
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jan 25 '07

re: confusion in search and replace..


It is replacing ;) first so when the double wink replace function tries replacing all the double winks have already been replaced by ;wink. The simple solution for this and for any similar problem where you have a two replace functions and one replace string is part of another replace string (as in this case double wink contains wink), always perform the larger string replace first.

In other words, swap them round and it will work:
Expand|Select|Wrap|Line Numbers
  1. mytext = mytext.replace(/\;;\)/ig,"DBL_wink")
  2. mytext = mytext.replace(/\;\)/ig,"wink")
  3.  
Newbie
 
Join Date: Jan 2007
Posts: 2
#3: Jan 26 '07

re: confusion in search and replace..


In other words, swap them round and it will work:
Expand|Select|Wrap|Line Numbers
  1. mytext = mytext.replace(/\;;\)/ig,"DBL_wink")
  2. mytext = mytext.replace(/\;\)/ig,"wink")
  3.  
[/quote]

Thanks worked superb
Reply