Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

changing $ to a different character in javascript variable

 
Rohit Sajan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In javascript i want to replace $ with another character ; , but when i used the normal replace moethod it's not happening. Is $ having any specific meaning in regular expression. My requirement is replace sim$san with sim;san
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$ means the end of the line. So you must escape it using '\$'
 
Ashish K Sharma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have wrote a sample function and it returns perfectly fine value

js code::
********************
var inParam = "sim$sin";
var outParam = inParam.replace("$",";");
alert(outParam);
***********************

Do let me know if you are trying something else or you have some other requirement.
 
Rohit Sajan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forgot to mention that $ sign occurance comes multipe times in the variable. If it is only one time then simple replace comes.

eg: sim$san$sin$sam should come like this sim;san;sin;sam

I tried like this finalVals=finalVals.replace(/$/g,';');
but not working. For other characters it works fine
[ August 25, 2006: Message edited by: Saju Pappachen ]
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Satou kurinosuke already mentioned, $ is a special character so you will have to use \$ to do the replacements. Here's the code:


 
Rohit Sajan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes jaikiran I tried the option suggested by both of you. Still it is not changing the $ characters
 
Ashish K Sharma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Can you please send what code you are trying and in which format/ which field you what to populate the replaced string?That would be helpful to identify.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic