5 Replies - 549 Views - Last Post: 20 July 2012 - 05:03 PM

#1 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

string.replace() problems

Posted 20 July 2012 - 05:49 AM

Hi... having problems with simple JS again -.-

if (result.indexOf('-.')>-1) {
            result.replace(/\-\./, '-');
    }

This should replace a string -.123.123 to -123.123 shouldnt it?

Tried other ways too, such as:
if (result.indexOf('-.')>-1) {
            result.replace('-.', '-');
    }


dont get it why it doesnt work -.-

This post has been edited by Toxicterror: 20 July 2012 - 05:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: string.replace() problems

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: string.replace() problems

Posted 20 July 2012 - 06:17 AM

Yes, it will (and does); but you're not storing the output anywhere.

I think you're looking for result = result.replace('-.', '-');
Was This Post Helpful? 1
  • +
  • -

#3 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: string.replace() problems

Posted 20 July 2012 - 06:50 AM

hmm why does JS just dont sound logic to me...
sometimes it need to assign it to a variable, sometimes it doesnt :(

i'll give it a try and hope it'll work :)
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: string.replace() problems

Posted 20 July 2012 - 07:11 AM

Well, welcome to the wonderful world of Javascript :)

But for the most part if you're modifying a single instance object by calling a method on it then you'll probably be getting a new one returned.

If you're using a method (such as Array.Sort or delete) then it will modify the variable that you pass into it.

Of course there are exceptions to that.
Was This Post Helpful? 0
  • +
  • -

#5 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: string.replace() problems

Posted 20 July 2012 - 11:20 AM

View PostToxicterror, on 20 July 2012 - 07:20 PM, said:

hmm why does JS just dont sound logic to me...
sometimes it need to assign it to a variable, sometimes it doesnt :(

i'll give it a try and hope it'll work :)


That is because strings in JS are not mutable i.e. cannot be changed.

Therefore any operation on them results in a new string (with the operation applied) and so you need to store that string somewhere to use.

This post has been edited by chinchang: 20 July 2012 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#6 Toxicterror  Icon User is offline

  • blub

Reputation: 15
  • View blog
  • Posts: 687
  • Joined: 10-March 09

Re: string.replace() problems

Posted 20 July 2012 - 05:03 PM

as for now on i'll just assign everything to a "new" variable ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1