Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Javascript Tutorial - Leave a comment regarding this online tutorial

9 views
Skip to first unread message

Javascript replace

unread,
Apr 14, 2012, 11:00:49 PM4/14/12
to
Just posted a tutorial on the Javascript Replace method and need you to point out whether is there any error in that tutorial or not @ http://gadgets-code.com/javascript-replace-method-review so I can further improve on that piece of tutorial and provide as accurate information as possible the the online Javascript lovers. Thanks. :)

Thomas 'PointedEars' Lahn

unread,
Apr 15, 2012, 4:19:24 AM4/15/12
to
0. You should have posted using your real name to avoid your posting being
misinterpreted as spam.

0.1. You should not have posted using Google Groups, from which most spam
messages to Usenet are injected nowadays.

0.2. You should have made yourself aware of the other shortcomings of
Google Groups with regard to Network News, such as lack of proper
automatic line-break. Your posting consists of a single line of 335
characters, which is inacceptable for plain-text Network News,
including Usenet (where you have posted to).

Postings should be properly formatted and lines should not exceed 80
characters per line. One recommendation is to format the line so that
it ends before the 76th character, to that there is room for quotation
characters (`> > '). See also <http://jibbering.com/faq/#posting> pp.

1. There is no "Javascript" (or "javascript"). You and your readers need to
become aware that they are writing code that is executable in and
(because of different host environments, necessarily) executed by
several different implementations of ECMAScript that differ from one
another:

<http://PointedEars.de/es-matrix>

2. Improve your code style so that your readers learn how to write easily
readable (and thus inexpensively maintainable) code. The comma should be
followed by at least one space character.

3. Verbatim string literals may not be continued on the following line
(some ECMAScript implementations, especially those conforming to Edition
5, support continuing a string literal on the following line which has
been delimited with a `\' character on the preceding line. See section
7.8.4.).

This code of yours does not _compile_:

var text = "You will replace him with new word, so go ahead and type
any word into the input box now!";

Modify your examples so that they compile and run as they are when copy
pasted. This can be accomplished best here with using string
concatenation, slicing the one string value into two so that the line
length does not exceed a reasonable common limit (such as 67 characters,
which is the prevailing standard for fixed-width printouts):

var text = "You will replace him with new word, so go ahead"
+ " and type any word into the input box now!";

4. Use window.alert(…) instead of alert(…), to make explicit that you intend
to call a method of the object referred to by the `window' property
of the global object, which does _not_ belong to any programming language
(anymore; but to the AOM/DOM API instead). The same applies to
window.prompt(…).

5. Your definition of "word" is incorrect or your description of the replace
example is imprecise.

text.replace(/him/gi,repText)

will _return_ the value of `text' with all _case-insensitive_
_occurences_ (substrings) of "him" replaced by the value of `repText'.
(The string value of `text' will remain _unchanged_. String values
are _immutable_ in ECMAScript implementations.)

A word, on the other hand, is usually defined as a substring delimited by
the start of input (or line), the end of input (or line), and/or non-word
characters (such as white-space). Whereas a word character is usually
defined (and so by the `\w' and `\b' escape sequences in ECMAScript) to
include the alphanumeric ASCII characters and the `_' (underscore)
character (/\w/ ~ /[0-9A-Za-z_]/). That is, the return value of

"foobarbaz".replace(/bar/gi, "bla")

is

"fooblabaz"

whereas "bar" surely cannot be considered a word in any sense, but a
_substring_ instead.

6. Reconsider your use of the term "RegExp object". There is only one
`RegExp' object in (an) ECMAScript (implementation). Objects created
using the `RegExp' constructor/factory object (i. e., `new RegExp(…)' or
`RegExp(…)') are thus called "RegExp _instances_' in the pertaining
Editions of the ECMAScript Language Specification. See for example
Edition 5, section 15.10.7.

7. window.location.replace(…), i. e. the replace(…) method of the location
object as referred to by the `location' property of window objects has
nothing to do with all this. Location objects are part of the
AOM (Application Object Model) API provided by the host environment
(a web browser, usually), not any programming language (anymore).

If you think that method should be mentioned in that article at all, make
it very clear that this method _has nothing to do_ with the method that
you discussed previously, and why, so that you do not to mislead your
readers.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Thomas 'PointedEars' Lahn

unread,
Apr 15, 2012, 4:36:22 AM4/15/12
to
Thomas 'PointedEars' Lahn wrote:

> 5. Your definition of "word" is incorrect or your description of the
> replace example is imprecise.
>
> text.replace(/him/gi,repText)
>
> will _return_ the value of `text' with all _case-insensitive_
> _occurences_ (substrings) of "him" replaced by the value of `repText'.
> (The string value of `text' will remain _unchanged_. String values
> are _immutable_ in ECMAScript implementations.)
>
> A word, on the other hand, is usually defined as a substring delimited
> by the start of input (or line), the end of input (or line), and/or
> non-word
> characters (such as white-space). Whereas a word character is usually
> defined (and so by the `\w' and `\b' escape sequences in ECMAScript) to
> include the alphanumeric ASCII characters and the `_' (underscore)
> character (/\w/ ~ /[0-9A-Za-z_]/). That is, the return value of
>
> "foobarbaz".replace(/bar/gi, "bla")
>
> is
>
> "fooblabaz"
>
> whereas "bar" surely cannot be considered a word in any sense, but a
> _substring_ instead.

Which reminds me that you have neglected to mention

- special substrings in the second string-value argument, such as "$&";
- the (most useful) use of a Function instance reference (callback) as
second argument (what I have said above only applies to non-function
values for that argument). For example:

/* "&#8709;&#322;&#8364;&#182;&#359;&#8592;&#8595;&#8594;&#960;" */
"∅ł€¶ŧ←↓→π".replace(
/./g,
function (match) {
return "&#" + match.charCodeAt(0) + ";";
})

/* "∅ł€¶ŧ←↓→π" */
"&#8709;&#322;&#8364;&#182;&#359;&#8592;&#8595;&#8594;&#960;".replace(
/&#(\d+);/g,
function (match, paren1) {
return String.fromCharCode(paren1);
})


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286...@94.75.214.39>

Javascript replace

unread,
Apr 15, 2012, 5:11:48 AM4/15/12
to
On Apr 15, 4:36 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Thanks, I will need some time to read through your comment and change
those mistakes accordingly.

Javascript replace

unread,
Apr 15, 2012, 5:27:43 AM4/15/12
to
On Apr 15, 4:19 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Javascript replace wrote:
> > Just posted a tutorial on the Javascript Replace method and need you to
> > point out whether is there any error in that tutorial or not @
> >http://gadgets-code.com/javascript-replace-method-reviewso I can further
Thanks, here are my answers

For comment number 3) : Actually there is only one line of code in my
text editor but due to space problem the text goes to second line when
shows in the front end, will include the '+' for the text in 3).
For comment number 4) : Really does not matter whether is
window.alert() or just alert(), you see we call 'document' directly
instead of window.document but will take a second look at it.
For comment 5 and 6 will need to read through your comments because it
is very long.

I will inform you when the corrections have been made on my post.

Thomas 'PointedEars' Lahn

unread,
Apr 15, 2012, 5:44:00 AM4/15/12
to
Javascript replace wrote:
^^^^^^^^^^^^^^^^^^
Who?

> Thomas 'PointedEars' Lahn wrote:
>> […]
>> 4. Use window.alert(…) instead of alert(…), to make explicit that you
>> intend to call a method of the object referred to by the `window'
>> property of the global object, which does _not_ belong to any programming
>> language (anymore; but to the AOM/DOM API instead). The same applies to
>> window.prompt(…).
>> […]
>
> Thanks, here are my answers

Please learn to quote (to *post*, really). Full-quoting the entire text
(including the signature) and adding your answers all below that is not
appropriate. You have been referred to the FAQ of this newsgroup already.
Further disregard of this most basic rule of posting will result in your
postings being ignored by me. Free time and other resources are too
precious to be wasted like this.

> […]
> For comment number 4) : Really does not matter whether is
> window.alert() or just alert(), you see we call 'document' directly
> instead of window.document but will take a second look at it.

Wrong on all accounts.

First, it does matter that you use `window' because when you do that you are
not relying on the global object having been augmented by the host
environment with an `alert' property (only a `window' property), or on the
host environment providing the necessary scope chain (in event-handler
attribute values). Keep in mind that host environments differ from one
another as the ECMAScript implementations they support differ from one
another.

Second, the object referred to by the `document' property is not involved
here. That argument may in itself be valid (tests are pending), but in this
context it is a fallacy.

Third, you are _not_ calling anything then; `document' is a property access
(implicitly, by identifier resolution along the scope chain), not a
(function) call. Mind your terminology unless you want to be written off as
a hopeless script-kiddie.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Evertjan.

unread,
Apr 15, 2012, 11:39:27 AM4/15/12
to
"and need you to point out"?
"whether is there"?

Perhaps "would like you to point out whether there is" is more becoming?
And please sign with preferably your name or a nickname if you want a
serious discussion. I would not like to discuss this with "Javascript
replace".

A tutorial can be without errors,
but not covering the subject in it's totality at all.

> var newText = oldText.replace(new RegExp("this","g"),"my");

Why this example?

Do you have enough knowledge of regex to write such "tutorial"?
Try for use and comment:

var newText = oldText.replace(/^.{9}th.s\st[xe]{2}t/g,"my words");

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

Dr J R Stockton

unread,
Apr 16, 2012, 3:10:14 PM4/16/12
to
In comp.lang.javascript message <1399409.A...@PointedEars.de>,
Sun, 15 Apr 2012 10:19:24, Thomas 'PointedEars' Lahn
<Point...@web.de> posted:

>Javascript replace wrote:
>
> ...

TL omitted to insert section 0.3, on the social deficiencies commonly
found in those who post with gmail addresses.


The writing of worthwhile tutorials requires a degree of intellectual
maturity rarely achieved before the age of 40. It also requires a
considerable in-depth understanding of and around the topic of the
tutorial (not shown by the OP), in order to be able to judge effectively
what should be included, what should be omitted, and what should be said
to exist but not described in full.

For example, in a "Javascript replace method review", after correcting
that title, one should first say which types of Object have the method
(some methods apply to more than one type of Object), how many arguments
it needs or can use, and of what Types they may be.

The first argument of the replace method does not have to be a RegExp;
the second does not have to be a String. Such an elementary tutorial
(which is unnecessary) should refer to the general capabilities that the
alternatives offer. In one case it is sufficient to mention the other
type; in the other, it is reasonable not to teach the other type, but
the reader should be made aware that more exists.

Each page of any JavaScript tutorial should link to ECMA 262 5.1, and
the specific sections most needed (e.g. 15.5.4.11) should be named.

But maybe the OP is not familiar with that document. See sig line 4.

--
(c) John Stockton, nr London UK ?@merlyn.demon.co.uk IE8 FF8 Op11 Sf5 Cr15
news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
<http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
******* <http://www.merlyn.demon.co.uk/quotings.htm#CPSnow> *******
0 new messages