JavaScript Functions

B

Bruce A. Julseth

I've been developing in PHP and now want to learn the client side with
JavaScript..

One of the first problems I have come across is what string functions are
available. Right now I'm looking for a "trim" funciton. Is the one? If not,
how can I do it?

Also, when my browser (IE, FireFox, Safari, Opera) finds a syntext problem,
it just plain quits executing with no error message. Is there a "switch" I
need to turn on in my browers?

Thank you....
 
T

Trevor Lawrence

Bruce A. Julseth said:
I've been developing in PHP and now want to learn the client side with
JavaScript..

One of the first problems I have come across is what string functions are
available. Right now I'm looking for a "trim" funciton. Is the one? If
not, how can I do it?

Also, when my browser (IE, FireFox, Safari, Opera) finds a syntext
problem, it just plain quits executing with no error message. Is there a
"switch" I need to turn on in my browers?

Thank you....


Hmm, an intersting quetsion
I had a look at http://www.w3schools.com/jsref/jsref_obj_string.asp
and there does not appear to be a trim function

A nice long winded way to left trim is to search for the first occurrence of
a non blank character and substring from that point, e.g.
<script type="text/javascript">
var str=" Hello world!", i, x ;
for (i = 0; i < str.length; i++) {
if(str.substr(i,1) != " ")
{ x=i; i = str.length; }
}
document.write(str.substr(x))
</script>

It would be similar for a right trim, using a reverse search, but I am
having trouble doing it

In any case, the experts here will no doubt find a better way to do it

Re syntax errors,
In IE7 try clicking the error icon on the bottom left. It doesn't always
help a great deal though.
Firefox has a plug-in named Firebug. Look for it on the FF site.
 
S

SAM

Le 12/27/08 3:37 AM, Bruce A. Julseth a écrit :
I've been developing in PHP and now want to learn the client side with
JavaScript..

One of the first problems I have come across is what string functions are
available. Right now I'm looking for a "trim" funciton. Is the one? If not,
how can I do it?

reg Expressions ?

// delete/suppress blank characters in beginning and end

function trim(strg) {
return strg.replace(/^\s+|\s+$/g, '');
}

Also, when my browser (IE, FireFox, Safari, Opera) finds a syntext problem,
it just plain quits executing with no error message. Is there a "switch" I
need to turn on in my browers?

Firefox : menu Tools / Errors Console
Safari : preferences / advanced / [X] active developpement menu
then : menu Developpement / Display console
 
S

SAM

Le 12/27/08 6:02 AM, SAM a écrit :
// delete/suppress blank characters in beginning and end

function trim(strg) {
return strg.replace(/^\s+|\s+$/g, '');
}


function trimLeft(strg) {
return strg.replace(/^\s+/, '');
}

function trimRight(strg) {
return strg.replace(/\s+$/, '');
}
 
R

RobG

Bruce A. Julseth said:
I've been developing in PHP and now want to learn the client side with

JavaScript..

One of the first problems I have come across is what string functions
are
available.

The authoritative reference for properties and methods of built-in
objects (such as String) is the ECMA-262 specification.

Right now I'm looking for a "trim" funciton. Is the one?

Not a built-in function. .

If not,
how can I do it?

There is one in the FAQ, which also has links to useful resources.
 
G

Gregor Kofler

Bruce A. Julseth meinte:
I've been developing in PHP and now want to learn the client side with
JavaScript..

One of the first problems I have come across is what string functions are
available. Right now I'm looking for a "trim" funciton. Is the one? If not,
how can I do it?

No. Besides it would be a method of the string (prototype) object.

function trim(yourString) {
return yourString.replace(/^\s+\s+$/, "");
}
Also, when my browser (IE, FireFox, Safari, Opera) finds a syntext problem,
it just plain quits executing with no error message. Is there a "switch" I
need to turn on in my browers?

Do yourself a favour and get proper add-ons (or activate) them. FF has
Firebug, Opera has Dragonfly (Extras->(whatever that menu entry is
called in English)->Developer Tools), Safari has somewhat less capable
but still sufficient tools (activated somewhere in the options dialog,
being on Linux I can't test that right now).

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Bruce A. Julseth meinte:

No. Besides it would be a method of the string (prototype) object.

function trim(yourString) {
return yourString.replace(/^\s+\s+$/, "");
}

Apparently you forgot a | between the two \s+, and the global flag. See the
FAQ entry.
Also, when my browser (IE, FireFox, Safari, Opera) finds a syntext problem,
it just plain quits executing with no error message. Is there a "switch" I
need to turn on in my browers?

Do yourself a favour and get proper add-ons (or activate) them. [...]
Opera has Dragonfly (Extras->(whatever that menu entry is called in
English)->Developer Tools), [...]

Speaking of which, does anyone know what I can do about always getting a
"Select a runtime" message when entering, say, 1 in the Command Line pane of
Dragonfly's Script tab in Opera/9.63 (X11; Linux i686; U; en) Presto/2.1.1?
So far I have found nothing relevant in the Settings (button with tools
icon, lower right corner of the Dragonfly pane).


TIA

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Gregor said:
Bruce A. Julseth meinte:
Also, when my browser (IE, FireFox, Safari, Opera) finds a syntext problem,
it just plain quits executing with no error message. Is there a "switch" I
need to turn on in my browers?
Do yourself a favour and get proper add-ons (or activate) them. [...]
Opera has Dragonfly (Extras->(whatever that menu entry is called in
English)->Developer Tools), [...]

Speaking of which, does anyone know what I can do about always getting a
"Select a runtime" message when entering, say, 1 in the Command Line pane of
Dragonfly's Script tab in Opera/9.63 (X11; Linux i686; U; en) Presto/2.1.1?
So far I have found nothing relevant in the Settings (button with tools
icon, lower right corner of the Dragonfly pane).

I think I've got it. For Scripts/Command Line to work properly, you have to
select a window/tab as global execution context under Scripts/Scripts first;
unlike Firebug, with Dragonfly the current tab is not automatically
selected. (I'd rather they changed that.)


PointedEars
 
G

Gregor Kofler

Thomas 'PointedEars' Lahn meinte:
Apparently you forgot a | between the two \s+, and the global flag. See the
FAQ entry.

Oops. Yes, I should re-read my posts...

Gregor
 
S

SAM

Le 12/27/08 10:30 PM, kangax a écrit :
RobG wrote:

[...]
If not,

There is one in the FAQ, which also has links to useful resources.

Perhaps, FAQ should mention that RegExp whitespace character class does
not conform to specification (see: 15.10.2.12) in some of the browsers
and that `trim` (as it is right now in the FAQ) will fail to remove some
of those characters.

Is &nbsp; or   or \xA0 a 'blank' (or white-space) character ?

My Firefox.3 and Opera.9 seem to think it is.
My iCab.4 and Safari.3 think it is not.
var s = ' \n\r\t\x0B\f\xA0 hello \xA0\n \r\t\f\x0B';
// should be "5"
document.write(trim(s).length);
fails in IE, Safari (although, fixed in webkit nightlies) and Chrome.

In IE ... I don't know.
A workaround is, of course, simple:

var strip = (function(){
var wspClass = '[\\x09\\x0B\\x0C\\x20\\xA0\\x0A\\x0D\\u2028\\u2029]';
var leadingSpace = new RegExp('^' + wspClass + '+');
var trailingSpace = new RegExp(wspClass + '+$');
return function(s) {
return s.replace(leadingSpace, '').replace(trailingSpace, '');
}
})();

Rest to know if really we want to delete this unbreakable character ?
 
T

Trevor Lawrence

Trevor Lawrence said:
Hmm, an intersting quetsion
I had a look at http://www.w3schools.com/jsref/jsref_obj_string.asp
and there does not appear to be a trim function

A nice long winded way to left trim is to search for the first occurrence
of a non blank character and substring from that point, e.g.
<script type="text/javascript">
var str=" Hello world!", i, x ;
for (i = 0; i < str.length; i++) {
if(str.substr(i,1) != " ")
{ x=i; i = str.length; }
}
document.write(str.substr(x))
</script>

It would be similar for a right trim, using a reverse search, but I am
having trouble doing it

In any case, the experts here will no doubt find a better way to do it

Re syntax errors,
In IE7 try clicking the error icon on the bottom left. It doesn't always
help a great deal though.
Firefox has a plug-in named Firebug. Look for it on the FF site.

While I appreciate the elegance of the RegExp solutions, there seemed to be
some problems with defining what is white space, so I wondered if the simple
functions below would do the job just as well

function ltrim(str) {
for (var i = 0; i < str.length; i++) {
if(str.substr(i,1)!=" ")
break;
}
return str.substr(i);
}

function rtrim(str) {
for (var i = str.length-1; i >= 0; i--) {
if(str.substr(i,1)!=" ")
break;
}
return str.substr(0,i+1);
}

function trim(str) {
return ltrim(rtrim(str));
}

They can be tested by
var stringToTrim = " Hello world! " ;
document.write("String: |" + stringToTrim + '|<br>' )
document.write("ltrim: |" + ltrim(stringToTrim) + '|<br>' )
document.write("rtrim: |" + rtrim(stringToTrim) + '|<br>' )
document.write("trim: |" + trim(stringToTrim) + '|<br>' )

I have placed the '|' delimiter at start and end of the string so that the
result can be clearly seen. This is what prints
String: | Hello world! |
ltrim: |Hello world! |
rtrim: | Hello world!|
trim: |Hello world!|
 
T

Thomas 'PointedEars' Lahn

Trevor Lawrence said:
While I appreciate the elegance of the RegExp solutions, there seemed
to be
some problems with defining what is white space, so I wondered if the
simple
functions below would do the job just as well
[...]

Dear green beginners,

simple functions like these is how this thing started about a decade
ago. Since they turned out to be inadequate (handle too few cases, are
inefficient from the outset, do not scale well), RegExp matching and
replacing was introduced.

That some ECMAScript implementations might not match all specified
white-space characters and line terminators with \s really is no reason
at all for us to prefer Wheel 0.1 that could only jump 1 cm (handle only
one white-space character, space, inefficiently aso.), because RegExp
allows us to define our own character classes.

Thank you in advance.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
, Sat, 27 Dec 2008 23:52:20, SAM <[email protected]
valid> posted:
Rest to know if really we want to delete this unbreakable character ?

The FAQ entry should list the characters that the simple code ought to
delete, and the known exceptions in reasonably common browsers. There,
"ought" could be according to 16262 or according to the majority of
usage.


The FAQ at Jibbering has not been changed for a month and is dated a
fortnight earlier. The auto-posted FAQ in this group is dated a month
earlier than that.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
While I appreciate the elegance of the RegExp solutions, there seemed to be
some problems with defining what is white space, so I wondered if the simple
functions below would do the job just as well

function ltrim(str) {
for (var i = 0; i < str.length; i++) {
if(str.substr(i,1)!=" ")
break;
}
return str.substr(i);
}

No point in coding like that. One can just modify the RegExp routines
in the FAQ by replacing "\s" with " " and changing "whitespace" to
"space(s) in the description."

AFAIK, all common browsers include in their "whitespace character = \s"
all the characters which normally need to be handled. The differing
characters can only appear if entered by a programmer, who can make
appropriate tests, or by an end user - and an end user who contrives to
enter an esoteric separator deserves whatever he gets.

The question about \xA0, however, is a good one that should be
considered for the FAQ entry.
 
S

SAM

Le 12/28/08 12:55 AM, Trevor Lawrence a écrit :
While I appreciate the elegance of the RegExp solutions, there seemed to be
some problems with defining what is white space, so I wondered if the simple
functions below would do the job just as well

Probably they search and eliminate simple white space : ' '
and that doesn't solve the problem to identify other spaces required in
a "trim" function (what to cut of ?).

function trimWhiteSpace(strg) {
return strg.replace(/^ +| +$/g, '');
}

document.write('['+trimWhiteSpace(" Hello world! ")+']');

--> [Hello world!]

function lTrimWS(strg) { return strg.replace(/^ +/, ''); }

function rTrimWS(strg) { return strg.replace(/ +$/, ''); }


If we refer to the PHP trim function(1) we would have to strip from
beginning and end the invisible following characters :
* " " (ASCII 32 (0x20)), an ordinary space.
* "\t" (ASCII 9 (0x09)), a tab.
* "\n" (ASCII 10 (0x0A)), a new line (line feed).
* "\r" (ASCII 13 (0x0D)), a carriage return.
* "\0" (ASCII 0 (0x00)), the NUL-byte.
* "\x0B" (ASCII 11 (0x0B)), a vertical tab.
whom unbreakable space is absent

what is supposed to almost get in reg expression(2) using: \s
witch would have to be equivalent with :

Gecko :
[\t\n\v\f\r
\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
(where \u00a0 is the unbreakable space)

Microsoft :
[\f\n\r\t\v] (and they forget ' ' !)


So we could have :

function trimer( strg, extendedBlanks ) {
var reg = '[\f\n\r\t\v ' + ( extendedBlanks? '\xa0' : '' ) + ']';
reg = new RegExp( '^' + reg + '+|' + reg + '+$', 'g' );
return strg.replace(reg,'');
}

And with the string :
var strng = ' \n\t \n \xa0 hello \xa0 \n \n';
We can get :
document.write('['+ trimer(strng) +']'); // [ hello ]
document.write('['+ trimer(strng,1) +']'); // [hello]

Tested : FF.3, Safari.3, Opera.9, iCab.4



(1)
<http://fr.php.net/manual/en/function.trim.php>
(2)
<http://msdn.microsoft.com/en-us/library/se61087k(VS.85).aspx>
<https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:RegExp>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top