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


Groups > comp.lang.javascript > #7366 > unrolled thread

String Replace. Please, need help.

Started by"Shapper." <mdmoura@gmail.com>
First post2011-10-13 12:42 -0700
Last post2011-10-18 19:49 +0200
Articles 15 — 9 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#7366 — String Replace. Please, need help.

From"Shapper." <mdmoura@gmail.com>
Date2011-10-13 12:42 -0700
SubjectString Replace. Please, need help.
Message-ID<11174912.1946.1318534920723.JavaMail.geo-discussion-forums@vbmh5>
Hello,

I am using "replace" as follows:

content = source.replace("{id}", options.id).replace("{title}", title);

However "source" might have or not have the strings "{id}" and "{title}".

When it does not have I get an error.

How can I solve this?

-- 
 
Thank You,
Miguel

[toc] | [next] | [standalone]


#7368

From"Evertjan." <exjxw.hannivoort@interxnl.net>
Date2011-10-13 21:17 +0000
Message-ID<Xns9F7DECFD08C7Beejj99@194.109.133.246>
In reply to#7366
Shapper. wrote on 13 okt 2011 in comp.lang.javascript:

> Hello,
> 
> I am using "replace" as follows:
> 
> content = source.replace("{id}", options.id).replace("{title}", title);
> 
> However "source" might have or not have the strings "{id}" and "{title}".

Use regular expressions in stead of a string.

> When it does not have I get an error.

Is that a question or a statement?
 
> How can I solve this?

What error do you get?
It seems to me that the replace() works fine 
even if there is nothing to replace.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#7380

From"Tom de Neef" <tdeneef@qolor.nl>
Date2011-10-14 09:43 +0200
Message-ID<4e97e81e$0$2518$e4fe514c@news2.news.xs4all.nl>
In reply to#7366
"Shapper." <mdmoura@gmail.com> schreef in bericht 
news:11174912.1946.1318534920723.JavaMail.geo-discussion-forums@vbmh5...
> Hello,
>
> I am using "replace" as follows:
> content = source.replace("{id}", options.id).replace("{title}", title);
> However "source" might have or not have the strings "{id}" and "{title}".
> When it does not have I get an error.

Long shot: is title defined? or should it be options.title ?
Tom 

[toc] | [prev] | [next] | [standalone]


#7399

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-10-14 21:01 +0200
Message-ID<9922755.oyzYreSCDY@PointedEars.de>
In reply to#7380
Tom de Neef wrote:

> "Shapper." <mdmoura@gmail.com> schreef […],
>> I am using "replace" as follows:
>> content = source.replace("{id}", options.id).replace("{title}", title);
>> However "source" might have or not have the strings "{id}" and "{title}".
>> When it does not have I get an error.
> 
> Long shot: is title defined? or should it be options.title ?

The `title' identifier would be resolved, or fail to be resolved, in any 
case.  The problem must be elsewhere, like a non-standard replace() method.  
And who knows what `source' refers to …


PointedEars
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [next] | [standalone]


#7404

FromDr J R Stockton <reply1141@merlyn.demon.co.uk>
Date2011-10-14 21:02 +0100
Message-ID<TbAb$2ChVJmOFwB5@invalid.uk.co.demon.merlyn.invalid>
In reply to#7366
In comp.lang.javascript message <11174912.1946.1318534920723.JavaMail.ge
o-discussion-forums@vbmh5>, Thu, 13 Oct 2011 12:42:00, Shapper.
<mdmoura@gmail.com> posted:

>I am using "replace" as follows:
>
>content = source.replace("{id}", options.id).replace("{title}", title);
>
>However "source" might have or not have the strings "{id}" and "{title}".
>When it does not have I get an error.
>How can I solve this?

Use
        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 != .

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#7419

From"Evertjan." <exjxw.hannivoort@interxnl.net>
Date2011-10-15 09:32 +0000
Message-ID<Xns9F7F75787FD54eejj99@194.109.133.246>
In reply to#7404
Dr J R Stockton wrote on 14 okt 2011 in comp.lang.javascript:

> In comp.lang.javascript message <11174912.1946.1318534920723.JavaMail.ge
> o-discussion-forums@vbmh5>, Thu, 13 Oct 2011 12:42:00, Shapper.
> <mdmoura@gmail.com> posted:
> 
>>I am using "replace" as follows:
>>
>>content = source.replace("{id}", options.id).replace("{title}", title);
>>
>>However "source" might have or not have the strings "{id}" and "{title}".
>>When it does not have I get an error.
>>How can I solve this?
> 
> Use
>         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.

as replace() should not give an error, when there is nothing to replace.

better try:

alert( content = source.replace("{id}", options.id) );
alert( content = X.replace("{title}", title) );

I suspect the error won't show till after ok-ing the alerts.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#7420

FromLuuk <Luuk@invalid.lan>
Date2011-10-15 11:47 +0200
Message-ID<be1om8-l5l.ln1@luuk.invalid.lan>
In reply to#7419
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);

-- 
Luuk

[toc] | [prev] | [next] | [standalone]


#7421

From"Evertjan." <exjxw.hannivoort@interxnl.net>
Date2011-10-15 10:04 +0000
Message-ID<Xns9F7F7AE315239eejj99@194.109.133.246>
In reply to#7420
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
========================



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#7422

FromLuuk <Luuk@invalid.lan>
Date2011-10-15 12:14 +0200
Message-ID<p13om8-0am.ln1@luuk.invalid.lan>
In reply to#7421
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

[toc] | [prev] | [next] | [standalone]


#7424

From"Evertjan." <exjxw.hannivoort@interxnl.net>
Date2011-10-15 12:21 +0000
Message-ID<Xns9F7F92260FC19eejj99@194.109.133.246>
In reply to#7422
Luuk wrote on 15 okt 2011 in comp.lang.javascript:

> 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.....

I do not think your approach will help, Luuk,
as the the OP must be in error if he ment that the replace() ever would 
give such an error.

I showed that if your s.indexOf(t) returns -1 the replace() still allows 
this withourt throwing an error.

So the OP should set temporary breakpoints, like alert(1), alert(2), etc,
and replace the source string with known temporary strings having and 
then not having the substring to be replaced, to determine where the 
error occurs.

That is the good old-fashioned way of debugging.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#7464

FromAndreas Bergmaier <andber93@web.de>
Date2011-10-15 22:49 +0200
Message-ID<j7crjt$jfv$1@news.albasani.net>
In reply to#7424
Evertjan. schrieb:

> ...set temporary breakpoints, like alert(1), alert(2), etc,
> and replace the source string with known temporary [objects],
 > to determine where the error occurs.
>
> That is the good old-fashioned way of debugging.

What would be a bad new-fashioned way of debugging?
console.log, or searching through anonymus functions in the error 
stacktrace?

  Andreas

[toc] | [prev] | [next] | [standalone]


#7465

FromAntony Scriven <adscriven@gmail.com>
Date2011-10-15 15:37 -0700
Message-ID<62cf8fa7-a938-4249-ad4d-bbd34ec3f8a4@q12g2000yqe.googlegroups.com>
In reply to#7464
On Oct 15, 9:49 pm, Andreas Bergmaier wrote:

 > Evertjan. schrieb:
 >
 > > ...set temporary breakpoints, like alert(1), alert(2),
 > > etc, and replace the source string with known temporary
 > > [objects], to determine where the error occurs. That is
 > > the good old-fashioned way of debugging.
 >
 > What would be a bad new-fashioned way of debugging?
 > console.log, or searching through anonymus functions in
 > the error stacktrace?

I don't know, but I suspect a sensible way to debug in this
instance is to wait for some sort of empirical response from
the OP. --Antony

[toc] | [prev] | [next] | [standalone]


#7468

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-10-16 01:08 +0200
Message-ID<1423122.CCzGCTE0C9@PointedEars.de>
In reply to#7464
Andreas Bergmaier wrote:

> Evertjan. schrieb:
>> ...set temporary breakpoints, like alert(1), alert(2), etc,
>> and replace the source string with known temporary [objects],
>> to determine where the error occurs.
>>
>> That is the good old-fashioned way of debugging.
> 
> What would be a bad new-fashioned way of debugging?
> console.log, or searching through anonymus functions in the error
> stacktrace?

alert-type debugging in today's runtime environments and of today's Web 
applications is sometimes feasible, but as a general approach extremely 
questionable.  Consider, for example, debugging an event listener for the 
`focus' event: by calling alert, the element in question could easily lose 
focus when alert() is called and receive it again after the alert() was 
OK'd, which calls the listener, which calls alert() etc. pp.  BTST.

console.log() does not have that disadvantage (but it has others, at least 
in Webkit), neither has the ES5 `debugger' keyword; however, both require 
changing the source code, which inevitably changes the position of the bug, 
potentially heisenbugging the problem and introducing more bugs.  There are 
enough debuggers for any widely distributed browser (and therefore, 
ECMAScript implementation and/or DOM implementation) with which you can set 
breakpoints (even conditional ones), and watch values and the stack trace to 
make sure this is very rarely needed to find a bug, if that.

However, in my experience surprisingly few developers seem to be proficient 
enough to use a debugger, let alone professional enough to set one up.  For 
an off-topic example, I have seen people *refusing* to install Zend Debugger 
or Xdebug (both are PHP debuggers) on their development server and resorting 
to a rather tedious echo/print_r/var_dump/exit approach instead when remote 
debugging is so very convenient with Eclipse PDT.  And I still don't get it.  
But it also appears to be true that the art of debugging is seldom taught in 
computer science classes, so this should be changed.


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
 -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

[toc] | [prev] | [next] | [standalone]


#7495

FromDr J R Stockton <reply1141@merlyn.demon.co.uk>
Date2011-10-16 19:30 +0100
Message-ID<rhM1xtERLymOFw14@invalid.uk.co.demon.merlyn.invalid>
In reply to#7419
In comp.lang.javascript message <Xns9F7F75787FD54eejj99@194.109.133.246>
, Sat, 15 Oct 2011 09:32:49, Evertjan. <exjxw.hannivoort@interxnl.net>
posted:

>Dr J R Stockton wrote on 14 okt 2011 in comp.lang.javascript:
>
>> In comp.lang.javascript message <11174912.1946.1318534920723.JavaMail.ge
>> o-discussion-forums@vbmh5>, Thu, 13 Oct 2011 12:42:00, Shapper.
>> <mdmoura@gmail.com> posted:
>>
>>>I am using "replace" as follows:
>>>
>>>content = source.replace("{id}", options.id).replace("{title}", title);
>>>
>>>However "source" might have or not have the strings "{id}" and "{title}".
>>>When it does not have I get an error.
>>>How can I solve this?
>>
>> Use
>>         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.
>
>as replace() should not give an error, when there is nothing to replace.
>
>better try:
>
>alert( content = source.replace("{id}", options.id) );
>alert( content = X.replace("{title}", title) );
>
>I suspect the error won't show till after ok-ing the alerts.


You appear to be assuming that you, a Netherlander who is pretty good at
understanding and writing proper English, has perfectly understood the
intended meaning of what has been written by someone who seems to be an
Iberian, or possibly an American.  A smart Iberian would have posted
bilingually, in good Iberian and in English - many of us here are, as
you exemplify, not monoglots.

In particular, we do not know for certain whether the OP got an error
message for that code from the script engine, or an error message from
the script engine later on, or an error report from some sort of
checking later on.

The OP should have given the exact error message, and enough code to
reproduce the problem, and the input used, and the outputs obtained and
desired..  The OP should also have said which browser(s) and which OS
were in use, just in case it matters.

Perhaps the OP had not read <http://www.jibbering.com/faq/index.html>,
which is UP.

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#7546

FromBart Lateur <bart.lateur@telenet.be>
Date2011-10-18 19:49 +0200
Message-ID<kter97918ch4o0hj1up8nf188ckth42su2@4ax.com>
In reply to#7366
Shapper. wrote:

>I am using "replace" as follows:
>
>content = source.replace("{id}", options.id).replace("{title}", title);
>
>However "source" might have or not have the strings "{id}" and "{title}".
>
>When it does not have I get an error.
>
>How can I solve this?

Somehow I feel that the cause of your error may not be what you think it
is. But I do doubt if "source" is actually a string, which is necessary
for replace() to work.

You can always throw in an extra ".toString()" to make absolutely sure.

-- 
	Bart Lateur
	bart.lateur@telenet.be

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web