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.
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>