String Replace. Please, need help.

Discussion in 'Javascript' started by Shapper., Oct 13, 2011.

  1. Shapper.

    Shapper. Guest

    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
     
    Shapper., Oct 13, 2011
    #1
    1. Advertising

  2. Shapper.

    Evertjan. Guest

    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)
     
    Evertjan., Oct 13, 2011
    #2
    1. Advertising

  3. Shapper.

    Tom de Neef Guest

    "Shapper." <> 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
     
    Tom de Neef, Oct 14, 2011
    #3
  4. Tom de Neef wrote:

    > "Shapper." <> 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
     
    Thomas 'PointedEars' Lahn, Oct 14, 2011
    #4
  5. In comp.lang.javascript message <11174912.1946.1318534920723.JavaMail.ge
    o-discussion-forums@vbmh5>, Thu, 13 Oct 2011 12:42:00, Shapper.
    <> 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.
     
    Dr J R Stockton, Oct 14, 2011
    #5
  6. Shapper.

    Evertjan. Guest

    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.
    > <> 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)
     
    Evertjan., Oct 15, 2011
    #6
  7. Shapper.

    Luuk Guest

    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
     
    Luuk, Oct 15, 2011
    #7
  8. Shapper.

    Evertjan. Guest

    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)
     
    Evertjan., Oct 15, 2011
    #8
  9. Shapper.

    Luuk Guest

    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
     
    Luuk, Oct 15, 2011
    #9
  10. Shapper.

    Evertjan. Guest

    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)
     
    Evertjan., Oct 15, 2011
    #10
  11. 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
     
    Andreas Bergmaier, Oct 15, 2011
    #11
  12. 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
     
    Antony Scriven, Oct 15, 2011
    #12
  13. 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$> (2004)
     
    Thomas 'PointedEars' Lahn, Oct 16, 2011
    #13
  14. In comp.lang.javascript message <Xns9F7F75787FD54eejj99@194.109.133.246>
    , Sat, 15 Oct 2011 09:32:49, Evertjan. <>
    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.
    >> <> 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.
     
    Dr J R Stockton, Oct 16, 2011
    #14
  15. Shapper.

    Bart Lateur Guest

    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, Oct 18, 2011
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    594
    Big Brian
    Oct 14, 2003
  2. Alun
    Replies:
    3
    Views:
    4,519
    Masudur
    Feb 18, 2008
  3. Prasad S
    Replies:
    2
    Views:
    235
    Dr John Stockton
    Aug 27, 2004
  4. Ron M.
    Replies:
    4
    Views:
    130
    Tad McClellan
    Jan 5, 2004
  5. V S Rawat
    Replies:
    5
    Views:
    308
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page