Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > String Replace. Please, need help.

Reply
Thread Tools

String Replace. Please, need help.

 
 
Shapper.
Guest
Posts: n/a
 
      10-13-2011
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
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      10-13-2011
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)
 
Reply With Quote
 
 
 
 
Tom de Neef
Guest
Posts: n/a
 
      10-14-2011
"Shapper." <(E-Mail Removed)> 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


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-14-2011
Tom de Neef wrote:

> "Shapper." <(E-Mail Removed)> 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
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      10-14-2011
In comp.lang.javascript message <11174912.1946.1318534920723.JavaMail.ge
o-discussion-forums@vbmh5>, Thu, 13 Oct 2011 12:42:00, Shapper.
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-15-2011
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.
> <(E-Mail Removed)> 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)
 
Reply With Quote
 
Luuk
Guest
Posts: n/a
 
      10-15-2011
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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-15-2011
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)
 
Reply With Quote
 
Luuk
Guest
Posts: n/a
 
      10-15-2011
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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-15-2011
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
map question - need to find a string from an int but also need the int from the string? Angus C++ 3 05-03-2008 02:27 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments