Click to See Complete Forum and Search --> : A little help to figure this out


sniper
11-15-2005, 08:45 AM
As some of you ay know, I am on an internship right now and they have given me the task of removing javascript from an entire site and replacing it with asp. Here's the code:

function trim(str)
str = this != window? this : str
return str.replace(/^\s+/g, "").replace(/\s+$/g, "")
end function

I have to change this to asp, the only problem is that "?" is really confusing me. I know what "?" do in regular expresions but that line doesn't look like a regex to me. How would I convert that?

Giskard
11-15-2005, 09:22 AM
I can't explain all the parts of the function, but this is a "Cut and Paste" function that is used to duplicate in JavaScript what is done in the ASP function TRIM. So in your ASP you don't need to rewrite this code, just use the intrinsic TRIM function.

If you want more information on this function do a google search on "str = this != window? this : str" (with the quotes) and you'll get 53 english matches (155 total)

buntine
11-16-2005, 09:29 PM
str = this != window? this : str

This is a condensed version of a conditional statement (if, then, else) that is derived from C.

It works like this:

expression ? TRUE STATEMENT : FALSE STATEMENT;

If the expression is true, the first statement will be executed (the one directly after the "?". Otherwise, the second statement will be executed (the ont after the ":").

In this case, the conditional statement is just assigning a value to a variable based on the data type of the passed parameter. It could be rewritten as follows:

if (this != window)
{
str = this;
}


Regards.