| Author |
changing $ to a different character in javascript variable
|
Rohit Sajan
Ranch Hand
Joined: Jun 15, 2006
Posts: 34
|
|
|
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
Joined: Nov 24, 2005
Posts: 14669
|
|
|
$ means the end of the line. So you must escape it using '\$'
|
[My Blog]
All roads lead to JavaRanch
|
 |
Ashish K Sharma
Ranch Hand
Joined: Aug 22, 2006
Posts: 31
|
|
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
Joined: Jun 15, 2006
Posts: 34
|
|
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
Joined: Jul 20, 2005
Posts: 8141
|
|
As Satou kurinosuke already mentioned, $ is a special character so you will have to use \$ to do the replacements. Here's the code:
|
[My Blog] [JavaRanch Journal]
|
 |
Rohit Sajan
Ranch Hand
Joined: Jun 15, 2006
Posts: 34
|
|
|
yes jaikiran I tried the option suggested by both of you. Still it is not changing the $ characters
|
 |
Ashish K Sharma
Ranch Hand
Joined: Aug 22, 2006
Posts: 31
|
|
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.
|
 |
 |
|
|
subject: changing $ to a different character in javascript variable
|
|
|