String Replace. Please, need help.

S

Shapper.

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?
 
E

Evertjan.

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

Tom de Neef

Shapper. said:
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
 
T

Thomas 'PointedEars' Lahn

Tom said:
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
 
D

Dr J R Stockton

In comp.lang.javascript message <11174912.1946.1318534920723.JavaMail.ge
o-discussion-forums@vbmh5>, Thu, 13 Oct 2011 12:42:00, Shapper.
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 != .
 
E

Evertjan.

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.


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:

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

Luuk

Yes, but that would not solve this:



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);
 
E

Evertjan.

Luuk wrote on 15 okt 2011 in comp.lang.javascript:
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
========================
 
L

Luuk

Luuk wrote on 15 okt 2011 in comp.lang.javascript:


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

Evertjan.

Luuk wrote on 15 okt 2011 in comp.lang.javascript:
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.
 
A

Andreas Bergmaier

Evertjan. said:
...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
 
A

Antony Scriven

Evertjan. said:
...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
 
T

Thomas 'PointedEars' Lahn

Andreas said:
Evertjan. said:
...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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Dr J R Stockton wrote on 14 okt 2011 in comp.lang.javascript:


Yes, but that would not solve this:


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

Bart Lateur

Shapper. said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top