In js, eval () function and trim () are removed from the left and right spaces of strings.

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.