StatCounter User Forum  
StatCounter Free web tracker and counter

Go Back   StatCounter User Forum > Webmaster > Lounge (non-StatCounter related topics here!)

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 03-07-2007, 08:39 AM
nem-la nem-la is offline
Junior Member
 
Join Date: Jan 2007
Posts: 5
Default JS driving me crazy!

Hi there, I am getting more crazy & frustrated the more I try with this bit of code I am trying to get working! I am by no means a coder, and am trying to get some javascript working. I basically have some variables being injected into a page, which I need to then change, by finding ',' and replacing with '.' - I have been playing with regexp but I haven't found any reference to these characters - anyone know a quick (and easy!) way of doing this on the fly?

TIA!!!
  #2  
Old 03-07-2007, 02:44 PM
jonra01 jonra01 is offline
Master Member
 
Join Date: Feb 2005
Location: Mississippi
Posts: 3,607
Default

Replacing characters in a string is fairly easy with JS. This is a simple example. It would probably be best to set it up as a function that you can call whenever you need it. Here's a demo - http://www.jonra.com/css-demos/test/replace.html

<script type="text/javascript">
var str = "777,777,777";
newstr = str.replace(",",".","gi");
document.write(newstr);
</script>

The key function here is 'replace'. It takes the variable, which in this case is 'str' and replaces all instances of the defined character with the second character specified. In this case, a comma and a dot. The 'gi' is for global replacement. If you leave that out of the equation it only replaces the first instance.

You could eliminate one step in the script by writing it as - document.write(str.replace(",",".","gi")); instead of first assigning it to a new variable as I did in the script.

Last edited by jonra01; 03-07-2007 at 07:11 PM.
  #3  
Old 03-09-2007, 09:19 AM
nem-la nem-la is offline
Junior Member
 
Join Date: Jan 2007
Posts: 5
Default Thanks!

Perfecto! Thanks for the help, hopefully my sanity will return now!!!
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:18 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.