Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #7422

Re: String Replace. Please, need help.

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!news.xs4all.nl!not-for-mail
Date Sat, 15 Oct 2011 12:14:54 +0200
From Luuk <Luuk@invalid.lan>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1
MIME-Version 1.0
Newsgroups comp.lang.javascript
Subject Re: String Replace. Please, need help.
References <11174912.1946.1318534920723.JavaMail.geo-discussion-forums@vbmh5> <TbAb$2ChVJmOFwB5@invalid.uk.co.demon.merlyn.invalid> <Xns9F7F75787FD54eejj99@194.109.133.246> <be1om8-l5l.ln1@luuk.invalid.lan> <Xns9F7F7AE315239eejj99@194.109.133.246>
In-Reply-To <Xns9F7F7AE315239eejj99@194.109.133.246>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Message-ID <p13om8-0am.ln1@luuk.invalid.lan> (permalink)
Lines 55
NNTP-Posting-Host 80.101.116.74
X-Trace 1318673702 news2.news.xs4all.nl 2414 80.101.116.74:33066
Xref x330-a1.tempe.blueboxinc.net comp.lang.javascript:7422

Show key headers only | View raw


On 15-10-2011 12:04, Evertjan. wrote:
> Luuk wrote on 15 okt 2011 in comp.lang.javascript:
>
>> On 15-10-2011 11:32, Evertjan. wrote:
>>>>           X = source.replace("{id}", options.id)
>>>>>            content = X.replace("{title}", title)
>>>>>   and then you can test that in each case a change has been made, by
>>>>>   comparing the strings with == or != .
>>> Yes, but that would not solve this:
>>>
>>>>>>>   However "source" might have or not have
>>>>>>>   the strings "{id}" and "{title}".
>>>>>>>   When it does not have I get an error.
>>
>> function myReplace(s, t, u) {
>>       if s.indexOf(t)>=0 {
>>            return s.replace(t,u);
>>      } else {
>>            return s;
>>      }
>> }
>>
>> X = myReplace(source,"{id}", options.id);
>> content = myReplace (X, "{title}", title);
>
> Please explain your improvement, Luuk.
>
> I don't get an error here:
>
> ========================
> var s = 'xxxx';
> var t = '{id}';
>
> alert(s.indexOf(t)); // -1
>
> var r = s.replace(t, 'blah');
>
> alert(r); // xxx
> ========================
>
>
>

The OP said:
However "source" might have or not have the strings "{id}" and 
"{title}". When it does not have I get an error.

So, adding a test if it does can help.

But when the 'normal'  replace does not give an error, it still is very 
hard to find out why the OP gets en error in that case.....


-- 
Luuk

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

String Replace. Please, need help. "Shapper." <mdmoura@gmail.com> - 2011-10-13 12:42 -0700
  Re: String Replace. Please, need help. "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-10-13 21:17 +0000
  Re: String Replace. Please, need help. "Tom de Neef" <tdeneef@qolor.nl> - 2011-10-14 09:43 +0200
    Re: String Replace. Please, need help. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-14 21:01 +0200
  Re: String Replace. Please, need help. Dr J R Stockton <reply1141@merlyn.demon.co.uk> - 2011-10-14 21:02 +0100
    Re: String Replace. Please, need help. "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-10-15 09:32 +0000
      Re: String Replace. Please, need help. Luuk <Luuk@invalid.lan> - 2011-10-15 11:47 +0200
        Re: String Replace. Please, need help. "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-10-15 10:04 +0000
          Re: String Replace. Please, need help. Luuk <Luuk@invalid.lan> - 2011-10-15 12:14 +0200
            Re: String Replace. Please, need help. "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-10-15 12:21 +0000
              Re: String Replace. Please, need help. Andreas Bergmaier <andber93@web.de> - 2011-10-15 22:49 +0200
                Re: String Replace. Please, need help. Antony Scriven <adscriven@gmail.com> - 2011-10-15 15:37 -0700
                Re: String Replace. Please, need help. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-16 01:08 +0200
      Re: String Replace. Please, need help. Dr J R Stockton <reply1141@merlyn.demon.co.uk> - 2011-10-16 19:30 +0100
  Re: String Replace. Please, need help. Bart Lateur <bart.lateur@telenet.be> - 2011-10-18 19:49 +0200

csiph-web