Wim Roffal wrote:
[color=blue]
> Is there a possibility to do a string replace in javascript without regular
> experessions. It feels like using a hammer to crash an egg.
>
> Wim[/color]
Compare:
str = str.replace(/_/g, " ");
with:
var result = [];
for (var i = 0; i < str.length; i++) {
if (str.charAt(i) == "_") {
result.push(" ");
}else {
result.push(str .charAt(i));
}
}
str = result.join('') ;
And this was a simple example. Anything beyond a single character replacement
and it gets much more difficult, involving keeping track of substrings indices,
etc. String#replace( ) already does all this for you.
--
Grant Wagner <gwagner@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Grant Wagner <gwagner@agrico reunited.com> wrote in
news:410AA4A9.7 D330124@agricor eunited.com:
[color=blue][color=green]
>> Is there a possibility to do a string replace in javascript without
>> regular experessions. It feels like using a hammer to crash an egg.
>>
>> Wim[/color]
>
> Compare:
>
> str = str.replace(/_/g, " ");
>
> with:
>
> var result = [];
> for (var i = 0; i < str.length; i++) {
> if (str.charAt(i) == "_") {
> result.push(" ");
> }else {
> result.push(str .charAt(i));
> }
> }
> str = result.join('') ;
>
> And this was a simple example. Anything beyond a single character
> replacement and it gets much more difficult, involving keeping track
> of substrings indices, etc. String#replace( ) already does all this for
> you.[/color]
Alternatively, if you don't feel the urge to over-complicate the issue,
this does the same thing rather more easily:
str = str.split("_"). join(" ");
However, the replace method is still easier in most cases. If you do want
to avoid regular expressions (and avoiding unnecessary regexes is a worthy
objective), then you can always do:
Duncan Booth said:
[color=blue]
>However, the replace method is still easier in most cases. If you do want
>to avoid regular expressions (and avoiding unnecessary regexes is a worthy
>objective), then you can always do:
>
>String.prototy pe.replacestrin g = function(from, to) {
> return this.split(from ).join(to);
>}
>
>str = str.replacestri ng("_", " ");[/color]
However, split() takes a regular expression as the first
argument, so I don't think there's any real advantage
to using it over replace().
Lee <REM0VElbspamtr ap@cox.net> wrote in
news:ceg7r1025p j@drn.newsguy.c om:
[color=blue]
> Duncan Booth said:
>[color=green]
>>However, the replace method is still easier in most cases. If you do
>>want to avoid regular expressions (and avoiding unnecessary regexes is
>>a worthy objective), then you can always do:
>>
>>String.protot ype.replacestri ng = function(from, to) {
>> return this.split(from ).join(to);
>>}
>>
>>str = str.replacestri ng("_", " ");[/color]
>
> However, split() takes a regular expression as the first
> argument, so I don't think there's any real advantage
> to using it over replace().
>
>[/color]
split() can take either a string or a regular expression as the first
argument.
One advantage is that you don't have to worry about escaping the string
whereas with the replace method you do.
If the 'from' string is input from the user, and you don't want it treated
as a regular expression but as a simple string, then creating a regular
expression from the string requires extra work.
(I'm not saying that there isn't an easy way to do the last one, there
probably is, but my Javascript isn't that great and I can't think of one.
In Python I would use re.escape, but then Python can do string based
substitutions as well as regex based ones so I wouldn't have to.)
Note that the replace method does actually act as though it accepts a
string, so long as you don't want any flags. This works, although it only
does one replace:
JRS: In article <cee4ds$eag$1@r eader11.wxs.nl> , dated Fri, 30 Jul 2004
20:29:14, seen in news:comp.lang. javascript, Wim Roffal
<wimroffelpleas e@nospamm-planet.nl> posted :
[color=blue]
>Is there a possibility to do a string replace in javascript without regular
>experessions .[/color]
Yes : consider & test :- s = "abcde".replace ("c", "CCC")
Duncan Booth wrote:[color=blue]
> Lee <REM0VElbspamtr ap@cox.net> wrote [...][color=green]
>> Duncan Booth said:[color=darkred]
>>> However, the replace method is still easier in most cases. If you do
>>> want to avoid regular expressions (and avoiding unnecessary regexes is
>>> a worthy objective), then you can always do:
>>>
>>> String.prototyp e.replacestring = function(from, to) {
>>> return this.split(from ).join(to);
>>> }
>>>
>>> str = str.replacestri ng("_", " ");[/color]
>> [...][/color]
>
> In other words consider:
>
> str = str.replacestri ng("+", "-");
> str = str.replacestri ng("[", "(");
> str = str.replacestri ng(userinput1, userinput2);
>
> and the equivalent expressions using replace:
>
> str = str.replace(/\+/g, "-");
> str = str.replace(/\[/g, "(");
> str = str.replace(??? );
>
> (I'm not saying that there isn't an easy way to do the last one, there
> probably is, but my Javascript isn't that great and I can't think of one.
> In Python I would use re.escape, but then Python can do string based
> substitutions as well as regex based ones so I wouldn't have to.)[/color]
If, and only if, the user should not be allowed to enter regular
expressions, you are required to escape all special characters in
`userinput1' using String.prototyp e.replace before you can apply
it on String.prototyp e.replace() to replace itself. Quick hack:
BTW: ECMAScript/J(ava)Script can do string based substitutions as well --
just pass a string as first argument; but then you only get the first
occurrence of it replaced.
PointedEars
P.S.
me@privacy net is not an e-mail address ("A mailbox receives mail.") and
thus using it violates Netiquette (RFC 1855) and Internet/Usenet standards
(RFC 1036, 2822 among others) as well as most certainly the Acceptable Use
Policy of your Internet/Usenet service provider. Those who have told you
that using me@privacy.net does no harm to anyone but spammers have been
lying. <http://www.interhack.n et/pubs/munging-harmful/>
Thomas 'PointedEars' Lahn wrote:[color=blue]
> String.prototyp e.escapeRE = function(s)
> {
> return s.replace(/[]\\^$*+?.():=!|{ ,}[]/g, "\\$1");
> }
>
> var str = ...;
> ...
> str = str.replace(use rinput1.escapeR E(), userinput2);
>
> BTW: ECMAScript/J(ava)Script can do string based substitutions as well --
> just pass a string as first argument; but then you only get the first
> occurrence of it replaced.[/color]
Because of the latter, one must use a RegExp object as first argument:
Thomas 'PointedEars' Lahn wrote:
[color=blue]
>
> Because of the latter, one must use a RegExp object as first argument:
>
> var str = ...;
> ...
> str = str.replace(new RegExp(userinpu t1.escapeRE(), "g"), userinput2);[/color]
As with most user input, this could be problematic. Escaped characters
may throw a spanner in the works.
eval() may be used, though:
Mick White <mwhite13@BOGUS rochester.rr.co m> writes:
[color=blue]
> Thomas 'PointedEars' Lahn wrote:[/color]
[color=blue][color=green]
>> str = str.replace(new RegExp(userinpu t1.escapeRE(), "g"), userinput2);[/color]
>
> As with most user input, this could be problematic. Escaped characters
> may throw a spanner in the works.[/color]
I assume the call to "escapeRE" is meant to convert a string into a regular
expression that matches that string. It will *add* escape, so that the resulting
string is a valid RegExp that matches userinput1.
[color=blue]
> eval() may be used, though:[/color]
.... but shouldn't. Using eval("/"+string+"/g") is at best equivalent
to RegExp(string," g") if string is a valid RegExp, but can fail in much more spectacular
ways if string is not a RegExp. What if string was "x/;window.close() ;//" ?
[color=blue]
> S.replace(eval( "/"+userinput +"/g"), userinput2);[/color]
That is much less likely to work. It will fail for as simple a string as "["
as userinput.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> wrote in
news:410BC158.6 000704@PointedE ars.de:
[color=blue]
> BTW: ECMAScript/J(ava)Script can do string based substitutions as well
> -- just pass a string as first argument; but then you only get the
> first occurrence of it replaced.[/color]
Funny, thats exactly what I said in one of the paragraphs that you trimmed
from my posting.
[color=blue]
> P.S.
> me@privacy net is not an e-mail address ("A mailbox receives mail.")
> and thus using it violates Netiquette (RFC 1855) and Internet/Usenet
> standards (RFC 1036, 2822 among others) as well as most certainly the
> Acceptable Use Policy of your Internet/Usenet service provider. Those
> who have told you that using me@privacy.net does no harm to anyone but
> spammers have been lying.
> <http://www.interhack.n et/pubs/munging-harmful/>
>[/color]
me@privacy.net is a genuine e-email address which receives email that
nobody reads (although it does send a reply when it receives email). I use
it specifically because my Usenet service provider requires a valid email
address and suggests the use of me@privacy.net as a suitable valid address
where permission has been given to use it for this purpose.
Anyone trying to reply by email to my postings (using conforming software)
will, of course, have sent the reply to the reply-to address which will
reach me.
The document to which you refer list a variety of reasons why munging is
harmful. I agree with almost all of them: spammers do not see bounces (do I
care?); violating standards (which thankfully I'm not doing); more hassle
for innocent third parties (just as well the third party has given their
consent then); additional hassle for me (none); there is no silver bullet
(yup).
Duncan Booth wrote:[color=blue]
> Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> wrote in
> news:410BC158.6 000704@PointedE ars.de:
>[color=green]
>> BTW: ECMAScript/J(ava)Script can do string based substitutions as well
>> -- just pass a string as first argument; but then you only get the
>> first occurrence of it replaced.[/color]
>
> Funny, thats exactly what I said in one of the paragraphs that you
> trimmed from my posting.[/color]
Well, not exactly.
[color=blue][color=green]
>> P.S.
>> me@privacy net is not an e-mail address ("A mailbox receives mail.")
>> and thus using it violates Netiquette (RFC 1855) and Internet/Usenet
>> standards (RFC 1036, 2822 among others) as well as most certainly the
>> Acceptable Use Policy of your Internet/Usenet service provider. Those
>> who have told you that using me@privacy.net does no harm to anyone but
>> spammers have been lying.
>> <http://www.interhack.n et/pubs/munging-harmful/>[/color]
>
> me@privacy.net is a genuine e-email address which receives email that
> nobody reads (although it does send a reply when it receives email).[/color]
No, the RFCs state it plain and simple: "A mailbox receives mail."
(RFC 2822, 3.4. Address Specification) and "The 'From' line contains
the electronic mailing address of the person who sent the message,
in the Internet syntax." (RFC 1036, 2.1.1 From). me@privacy.net
receives nothing as there is no mail exchanger (MX record) configured
for the domain (and its subdomains, if it had any) and for the A record,
no SMTP server installed:
--------------------------------------------------------------------------
$ chkmadd -v me@privacy.net -t 120
chkmadd 0.1.2.200406211 6 -- (C) 2003, 2004 Thomas Lahn <mehl@PointedEa rs.de>
Distributed under the terms of the GNU General Public License (GPL).
See COPYING file or <http://www.fsf.org/copyleft/gpl.html> for details.
Report bugs to <chkmadd@Pointe dEars.de>.
Verifying <me@privacy.net > ...
Mail exchanger(s) for privacy.net:
None. Trying A record ...
privacy.net has address 66.98.244.52
chkmadd.exp 0.1.1.200407291 7 -- (C) 2004 Thomas Lahn <mehl@PointedEa rs.de>
Distributed under the terms of the GNU General Public License (GPL).
See COPYING file or <http://www.fsf.org/copyleft/gpl.html> for details.
Report bugs to <chkmadd.exp@Po intedEars.de>.
spawn telnet -- 66.98.244.52 smtp
Trying 66.98.244.52...
telnet: Unable to connect to remote host: Connection timed out
--------------------------------------------------------------------------
So me@privacy.net does *not* name a mailbox, it cannot.[1] Apart from that,
it does *not* belong to anyone. So it is therefore *no e-mail address* (to
be used in standards compliant headers).
I had this argument several times before (and -- isn't it characteristic? --
about the same reaction, although I must admit that yours appear to be the
most positive one), nuff said. However, consider yourself killfiled then
(as with my software I can only filter on the From and there are more people
abusing me@privacy.net who do not care for Reply-To.)
PointedEars
___________
[1] There were times were me@privacy.net returned a bounce that accused
the receiver of it to be a spammer and informed him/her that therefore
his/her mails were not read. It was a mailbox these times, yet a
asocial configured one. It was still bad as the mailbox did not belong
to anyone. Then they dropped this practice and removed the MX record
and the SMTP server as well, making it all worse (as described above).
Unfortunately, they are now lying about the bounce a receiver would
get on <http://privacy.net/email/>. There is no bounce from
privacy.net, it cannot be, as long there is no SMTP server. Instead
either the bounce is generated by the sending mail exchanger or the
e-mail cannot be even submitted (as the sending MX checks the receivers
"address" before, like my program does, and rejects it for the reasons
given).
Thomas 'PointedEars' Lahn <PointedEars@nu rfuerspam.de> wrote in
news:410D1003.7 020300@PointedE ars.de:
[color=blue][color=green][color=darkred]
>>> P.S.
>>> me@privacy net is not an e-mail address ("A mailbox receives mail.")
>>> and thus using it violates Netiquette (RFC 1855) and Internet/Usenet
>>> standards (RFC 1036, 2822 among others) as well as most certainly the
>>> Acceptable Use Policy of your Internet/Usenet service provider. Those
>>> who have told you that using me@privacy.net does no harm to anyone but
>>> spammers have been lying.
>>> <http://www.interhack.n et/pubs/munging-harmful/>[/color]
>>
>> me@privacy.net is a genuine e-email address which receives email that
>> nobody reads (although it does send a reply when it receives email).[/color]
>
> No, the RFCs state it plain and simple: "A mailbox receives mail."
> (RFC 2822, 3.4. Address Specification) and "The 'From' line contains
> the electronic mailing address of the person who sent the message,
> in the Internet syntax." (RFC 1036, 2.1.1 From). me@privacy.net
> receives nothing as there is no mail exchanger (MX record) configured
> for the domain (and its subdomains, if it had any) and for the A record,
> no SMTP server installed[/color]
Thats interesting. Sorry, I just went by what their web page claimed for
the address, I didn't check that it actually did what it claimed.
I've changed the From header line on my posting to use an address in the
..invalid domain. Feel free to send me chapter and verse on how this still
violates lots of RFCs (I can't see anything in RFC1855 that either .invalid
or me@privacy.net violates, and RFC 1036 doesn't actually say that the From
address must be valid), but at least you now have the option to killfile me
explicitly.
If you think the newsgroup has had enough of this thread, feel free to
email me directly, or not as you please.
Duncan Booth <duncan.booth@i nvalid.invalid> wrote in reply to Thomas
'PointedEars' Lahn in news:Xns9539679 CFB741duncanrcp couk@127.0.0.1:
[color=blue]
> [posted and mailed]
>[/color]
Except that Pointed Ears reply-to address bounced, so the mailed copy
didn't get through. :-)
JRS: In article <Xns953968BB361 81duncanrcpcouk @127.0.0.1>, dated Mon, 2
Aug 2004 09:26:59, seen in news:comp.lang. javascript, Duncan Booth
<duncan.booth@i nvalid.invalid> posted :[color=blue]
>Duncan Booth <duncan.booth@i nvalid.invalid> wrote in reply to Thomas
>'PointedEars ' Lahn in news:Xns9539679 CFB741duncanrcp couk@127.0.0.1:
>[color=green]
>> [posted and mailed][/color][/color]
That is generally considered to be ill-mannered, although substantially
less so when clearly marked as such.
[color=blue]
>Except that Pointed Ears reply-to address bounced, so the mailed copy
>didn't get through. :-)[/color]
Disregard all that he says about the composition of news articles. He
relies on some German document - such may be applicable by local consent
to the German hierarchy, but in this Big8 newsgroup only international
documents such as RFCs and Usefor drafts are of any value.
Comment