Home > Web Front-end > JS Tutorial > Application of eval() function and trim() in js to remove left and right spaces from string_javascript skills

Application of eval() function and trim() in js to remove left and right spaces from string_javascript skills

WBOY
Release: 2016-05-16 17:42:26
Original
1574 people have browsed it

Understand the eval() function in js and write a function trim() to remove the left and right spaces in the string.
trim() refers to the source code of jquery, you can use it with confidence.

My understanding of the eval() function in js is not necessarily correct.

Copy code The code is as follows:







There is no trim function in js to handle spaces before and after string filtering,

and jquery This very commonly used function has been added, and its source code is as follows

function trim(t){

return (t||"").replace(/^s |s $/ g, "");

}

Sometimes we don’t use jquery, there is no need to add the entire jquery library for a function,

At this time we can copy the source code Just write a trim function.



<script> <br>var f='hello'; <br>//alert(f); <br> /* <br>By the way, here is the eval() function, which can calculate the content in the brackets as a js script. <br> It can also calculate mathematical operations and string calculations. <br>In short, it is not a simple string concatenation function. <br>You can think of it as a js script in js. <br>This is very similar to jsp. JSP is the java code embedded in html. <br>The content in the eval() brackets is the js code embedded in js. <br>*/ <br>//eval("alert('" f "')");//Calculate the js script, which has the same effect as alert(f). <br>eval(" var gg='haha'"); <br>alert(eval("gg"));//The content in eval is the embedded js code, which is equivalent to var gg='haha', alert (gg); <br>//alert(eval('3 4'));//Calculate mathematical operations, the result is 7 <br>//alert(eval('3' '4'));//Calculate characters String, result 34 <br>alert("start" trim(' abc def ') "end"); <br>//The trim function in jquery filters out the first space. <br>function trim(t){ <br>return (t||"").replace(/^s |s $/g, ""); <br>} <br></script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template