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

May 16, 2016 pm 05:42 PM
eval trim

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does eval mean in python? What does eval mean in python? May 22, 2019 pm 03:18 PM

eval means "evaluation" and is a built-in function in Python that is used to execute a string expression and return the calculation result of the expression; that is, when assigning a variable, the representation on the right side of the equal sign is written in the format of a string, The return value is the result of this expression. Syntax "eval(expression[, globals[, locals]])".

How to disable eval in php under win How to disable eval in php under win Oct 31, 2022 am 09:33 AM

How to disable eval in PHP under win: 1. Download "PHP_diseval_extension"; 2. Find the PHP currently used by the server; 3. Open the PHP configuration file; 4. Add the "extension=diseval.so" code; 5. Restart the service.

Practical Tips: How to use the trim function in PHP to process Chinese spaces Practical Tips: How to use the trim function in PHP to process Chinese spaces Mar 27, 2024 am 11:27 AM

In PHP programming, spaces are often encountered when processing strings, including Chinese spaces. In actual development, we often use the trim function to remove spaces at both ends of a string, but the processing of Chinese spaces is relatively complicated. This article will introduce how to use the trim function in PHP to process Chinese spaces and provide specific code examples. First, let us understand the types of Chinese spaces. In Chinese, spaces include not only common English spaces (space), but also some other special spaces.

A simple explanation of the usage of eval in python A simple explanation of the usage of eval in python Mar 25, 2024 pm 02:08 PM

In Python, the eval() function is used to execute a string expression and return its result. It takes a string containing an expression as a parameter and evaluates the expression. The eval() function is powerful, but it should be noted that it will execute any valid Python expression contained in the string, so you should avoid accepting external input strings when using it to prevent security vulnerabilities.

Use the strings.Trim function to remove the specified character set from the beginning and end of a string Use the strings.Trim function to remove the specified character set from the beginning and end of a string Jul 24, 2023 pm 04:27 PM

Use the strings.Trim function to remove the specified character set at the beginning and end of a string. In the Go language, the strings.Trim function is a very practical function that can remove the specified character set at the beginning and end of a string, making the string more tidy and standardized. This article will introduce how to use the strings.Trim function and show some code examples. First, let’s take a look at the prototype of the strings.Trim function: funcTrim(sstring,cutsetstri

What is the function of trim function of solid state drive? What is the function of trim function of solid state drive? Nov 21, 2022 am 10:58 AM

The trim function of the solid-state drive is mainly to optimize the solid-state drive, solve the problem of slowdown and lifespan of the SSD after use, and improve the efficiency of the SSD by preparing data blocks for reuse. The Trim function is a function that almost all SSD solid state drives have. It is an ATA command. When the system confirms that the SSD supports Trim and deletes data, it does not notify the hard disk of the deletion command and only uses the Volume Bitmap to remember that the data here has been deleted. This enables faster data processing.

Guide to using trim() function in PHP Guide to using trim() function in PHP Feb 20, 2024 am 08:39 AM

Guide to using the trim() function in PHP The trim() function is very commonly used in PHP and is used to remove spaces or other specified characters at the beginning and end of a string. This article will introduce the use of trim() function in detail and provide specific code examples. 1. Function syntax The syntax of the trim() function is as follows: trim(string$str,string$character_mask=""):string This function accepts two parameters,

Use the PHP function 'trim' to remove whitespace characters at both ends of a string Use the PHP function 'trim' to remove whitespace characters at both ends of a string Jul 25, 2023 pm 04:45 PM

The PHP function "trim" is a very useful string processing function. It can help us remove whitespace characters at both ends of the string, including spaces, tabs, newlines, etc. When writing PHP programs, we often encounter situations where user input needs to be cleaned. At this time, using "trim" can ensure that we get a clean string and avoid errors caused by irregular user input. Here is a simple code example showing how to use the "trim" function: &lt;?php

See all articles