Understand eval () function in js and write a function trim () to remove spaces between the left and right strings.
Trim () is a reference to the jquery source code, you can rest assured to use.
My understanding of eval () functions in js is not necessarily correct.
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Title> New Document </title>
</Head>
<Body>
The trim function for processing spaces before and after string filtering in js does not exist. <br>
Jquery adds this commonly used function. The source code is as follows <br>
Function trim (t) {<br>
Return (t | ""). replace (/^ \ s + | \ s + $/g, ""); <br>
} <Br>
Sometimes we don't need jquery. We don't need to add the entire jquery library to a function. <br>
Now we can copy the source code to write a trim function. <Br>
</Body>
</Html>
<Script>
Var f = 'hello ';
// Alert (f );
/*
By the way, the eval () function can use the content in the brackets as js script computing,
It can also be used to calculate mathematical operations or strings.
In short, it is not a simple String concatenation function.
You can use it as a js script in js.
This is similar to jsp. jsp is the java code embedded in html,
The content in eval () is the js Code embedded in js.
*/
// Eval ("alert ('" + f + "')"); // calculate js scripts, which have the same effect as alert (f.
Eval ("var gg = 'hahaha '");
Alert (eval ("gg"); // The js Code embedded in eval is equivalent to var gg = 'hahaha', alert (gg );
// Alert (eval ('3 + 4'); // calculates the mathematical operation. Result 7
// Alert (eval ('3' + '4'); // calculate the string, result 34
Alert ("start" + trim ('abc def ') + "end ");
// Trim function in jquery to filter out the first space.
Function trim (t ){
Return (t | ""). replace (/^ \ s + | \ s + $/g ,"");
}
</Script>