Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help on replace using regular expression 1

Status
Not open for further replies.

edfimasa

Programmer
Apr 9, 2008
21
0
0
PT
Hi.

I have the following string "aaaaaa££aaaaa£aaaaa££"
and i want to replace £ for € and ££ for £.
(the string may change, but the rules are the same)

i've tried doing the following :
stringvar.replace(/[£]/g,"\u20AC")).replace(/[££]/g,"£")

It's obvious that the first replace replaces all £ by €..so the second replace never happens.

I'm trying to find a regular expression that helps me on this case, like saying to the replace that if he find a £ isolated from other £ he can replace for €. If not replace with £.

Hope you can help.

Cheers.
 
Use an intermediate step.

stringvar.replace(/££/g,'_').replace(/£/g, '€').replace(/_/g, '£');
 
Hi.
I can't use an intermediate step (or shouldn't), because in the text it can have that char.And that would mean more trouble.

Thank you anyway.
 
If the ? doubling meant to collapse mulitple ? to one instance, you can do this.
[tt] stringvar=stringvar.replace(/([^?])?([^?])/g,"$1€$2").replace(/£+/g,"£")[/tt]
 
The line encode bad! The [^?]?[^?] should be read [^£]£[^£], hope it shows up correctly this time.
 
Re-take again.

The line encode bad! The ([^?])?([^?]) should be read ([^£])£([^£]), hope it shows up correctly this time.
 
Hmmm cool, it going on the right track..
still

if i have this

bbbbba£aaaa£££aaa

it just goes nuts... but i guess it can't be helped.

anyways, my solution thanks to tsuji is
replace(/([^£])£([^£])/g,"$1€$2").replace(/£{2}/g,"£")

Thank you.
 
Just another question.

What about if i want to replace +euro+ by €

like in the following string :
"I have 1000+euro+ in my account and 10+euro+ in my wallet"

replace by
"I have 1000€ in my account and 10€ in my wallet"

Cheers.
 
\+euro+\ does the trick.

Thanks again.
 
Just add the replace like this.
[tt] .replace(/\+euro\+/gi,"€")[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top