[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Various points: Regexp's, history, scope, and many, many more!

Hiya... I'm new to this list, having mucked about with JS a little and
beaten my head against the wall waiting for more docs on it...

A list of things that have been bothering me for a while:

1) Lack of regular expressions (okay, okay, so I'm a Perl programmer)
The thing I love about using Perl for CGI is that regexps make verifying and
parsing user input *so easy*, and since one of JS's main uses is validating
form inputs, wouldn't regexps help a lot?
As an example, suppose I'm asking the user for a real number. I want to
validate it and put it in a variable. Users, being silly people, often type
spaces before or after their numbers, so we want to take account of that,
plus we want to parse this number to make sure it's a real and send an alert
if it's not. Also, we want to make sure there's nothing else in the string
apart rom the number.
Here's how it's done in Perl:

if ($input=~/^\s*(\d+\.\d+)\s*$/) {$real=$1} else {print "That's no real!"}

(What it means: /  - start/end exp
		^  - beginning of string/line
		\s - any spacing character (including tabs)
		*  - 0 or more of previous char
		(  - start grouping
		\d - digit
		+  - 1 or more of previous char
		\. - a . (has to be escaped, because . is a wildcard)
		)  - close grouping
		$  - end of string/line
So the whole thing means,
Match a string, the whole of which looks like:
	possibly some spaces
	some digits
	a .
	some more digits
	possibly some more spaces
	and that's all.
then group the digits with the dot in between together so they can be pulled
from $1.)

Now, the regexp itself may look like gibberish if you've never used them
before, but:
a) They are remarkably easy to learn and use, and
b) It's ONE LINE. As opposed to what has to be done in one of the Netscape
examples, which involves looping the length of the string, checking each
character individually, remembering what's gone before etc. I really don't
want to have to write one of those routines for every input box. (And yes,
I've seen that generic input-validation function, and it's not nearly as
As I said before, one of JS's main uses is for handling user input on the
client side. Using regexps, scripts can be made very robust very easily, since 
they allow you to handle a wide variety of input in a tiny amount of code.
JS needs to be made as easy and robust as possible. So wouldn't regular
expressions be a good idea?

(Sorry if I got carried away with the example above, I got trapped into
sherbet lemoning...)

Second point, much smaller: I assumed that the history object would contain
an array of URL objects, but it doesn't, despite having a .length property.
When's this getting fixed? I'd really like to be able to look at the last
URL visited, I've got a couple of cool applications in mind.

And a whole load of other questions...

What are the scope of user-created objects and functions? If create an
object in the top-level window, will child frames have access? 

The release notes for 2.0b5 for Unix say that <SCRIPT SRC="blah"> has been
enabled, anbd about time too... what's the situation with the other versions?

What's the story with crashes from calling functions in other windows? Does
this include calling functions in other frames?

When you do document.clear, what happens to any script code in that document?
Does it all need to be re-written? (That's why SRC= is so useful, I

Apparently, JS is what's used for Netscape Livewire's equivalent of CGI
scripting. That's all I know. Any more info?

Most of these questions are aimed at a particular app I have in mind... a JS
crossword. I've worked out quite a lot of it in my head already, just need
to get around to coding...

Sorry about the question overload, but this is my first post, I think
things'll get a lot easier from here on - at least, after my new book comes
out :)

-- Yoz

|  |,,-.---. Yoram Grahame - yoz@ucl.ac.uk , yoz@delphi.com
`--1 `-' ==; WWW: http://www.cs.ucl.ac.uk/students/Y.Grahame/
,-'|       ; "Right here, right now, friend, I'm as nervous as a cotton-pickin'
`--' ,----'   frog on a freeway with its hopper busted... I'm in bad shape."