Board index » javascript » what does this.value.replace().replace() do?

what does this.value.replace().replace() do?

2008-02-06 04:32:07 AM
Hello,
Amrit has posted the code below for keeping the focus on a text box
until some text is entered.
Can someone please explain how
{if(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") focus();}
works - I don't understand the
this.value.replace().replace() part....
Thanks
Geoff
function focus() {
if(typeof document.MyForm.username.onblur != "function")
document.MyForm.username.onblur = function()
{if(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") focus();}
alert("please add your first and last names before continuing");
document.MyForm.username.focus();
}
-
 

Re:what does this.value.replace().replace() do?

On Feb 6, 6:32 am, Geoff Cox <<>>wrote:
Quote
Hello,

Amrit has posted the code below for keeping the focus on a text box
until some text is entered.

Can someone please explain how

{if(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") focus();}
The focus method should be called on an object that is likely to
support it:
this.focus();
or more cautiously:
this.focus && this.focus();
Quote

works - I don't understand the

this.value.replace().replace() part....
this.value returns the value of the control, it is always a string.
.replace is a reference to the replace method of the string, it
returns a string.
(/^\s+/,"") calls the replace method with the enclosed arguments,
the first is a regular expression that matches one or more whitespace
characters at the start of the string. The second is the replacement
text, an empty string. The returned string will have all leading
whitespace removed.
.replace(/\s+$/,"") similar to the above, but removes all trailing
whitespace.
The test essentially determines if the value of the element consists
only of zero or more whitespace characters.
A shorter equivalent is:
if (/^\s*$/.test(this.value)) {
...
}
Rob
-

Re:what does this.value.replace().replace() do?

Geoff Cox < wrote on 05 feb 2008 in comp.lang.javascript:
Quote
Can someone please explain how

{if(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") focus();}

1 This part:
(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "")
is a silly long way of writing:
( !/\S/.test(this.value) )
============================
2 focus() must be appended to an object that supports it:
like
document.getElementById('theId').focus();
or
this.focus();
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
-

Re:what does this.value.replace().replace() do?

Geoff Cox < wrote:
Quote
Can someone please explain how

{if(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") focus();}

works - I don't understand the

this.value.replace().replace() part....
They're just consecutive calls. The this.value.replace() returns a
string. The second replace call is just operating on that string.
It's equivalent to this:
var x=this.value.replace();
var y=x.replace();
You could do something crazy like this:
var s="hello world";
alert(s.replace("hello","goodbye").replace("goodbye","hello"));
The first call would return a string of "goodbye world", and the second
call would return a string of "hello world" which would get alerted.
none of the calls would modify s though.
-

Re:what does this.value.replace().replace() do?

On Tue, 05 Feb 2008 22:22:53 +0100, Stevo <please@spam-me.com>wrote:
Quote
They're just consecutive calls. The this.value.replace() returns a
string. The second replace call is just operating on that string.
It's equivalent to this:

var x=this.value.replace();
var y=x.replace();

You could do something crazy like this:

var s="hello world";
alert(s.replace("hello","goodbye").replace("goodbye","hello"));

The first call would return a string of "goodbye world", and the second
call would return a string of "hello world" which would get alerted.
none of the calls would modify s though.
Thanks to all three of you - you've helped a lot!
Cheers
Geoff
-

Re:what does this.value.replace().replace() do?

On 05 Feb 2008 21:22:21 GMT, "Evertjan."
<exjxw.hannivoort@interxnl.net>wrote:
Quote
Geoff Cox < wrote on 05 feb 2008 in comp.lang.javascript:

Quote
Can someone please explain how

{if(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") focus();}


1 This part:

(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "")

is a silly long way of writing:

( !/\S/.test(this.value) )
I have just tried
function focus() {
if(typeof document.sliderForm.username.onblur != "function")
document.sliderForm.username.onblur = function()
{if( !/\S/.test(this.value) ) focus();}
alert("please add your first and last names before continuing");
document.sliderForm.username.focus();
}
with IE 7 it repeatedly brings up the alert when I click out of the
text box but with Firefox 2 this happens a couple of times and then I
can click outside the box but get no alert...can this be changed for
Firefox?
Cheers
Geoff
-

Re:what does this.value.replace().replace() do?

In comp.lang.javascript message <pohhq3t5cpaprgn2lei9lulfh0al8r185u@4ax.
com>, Tue, 5 Feb 2008 20:32:07, Geoff Cox <?@?.?.invalid>posted:
Quote
Can someone please explain how

{if(this.value.replace(/^\s+/,"").replace(/\s+$/,"") == "") focus();}

works - I don't understand the

this.value.replace().replace() part....
Read the Newsgroup FAQ, 4.16; <URL:www.merlyn.demon.co.uk/js-
valid.htm>; and what the latter links to.
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE6 IE7 FF2 Op9 Sf3
<URL:www.merlyn.demon.co.uk/js-index.htm>jscr maths, dates, sources.
<URL:www.merlyn.demon.co.uk/>TP/BP/Delphi/jscr/&c, FAQ items, links.
-