Home Web Front-end JS Tutorial js trim function, space removal function and regular collection_javascript skills

js trim function, space removal function and regular collection_javascript skills

May 16, 2016 pm 06:41 PM
trim

But if the project does not use a framework such as jQuery, js itself does not have such a function, and we have to write such a function ourselves. The following is the specific implementation of the function:

Copy the code The code is as follows:

//For users to call
function trim(s){
return trimRight(trimLeft(s));
}
//Remove the left whitespace
function trimLeft(s){
if(s == null) {
return "";
}
var whitespace = new String(" tnr");
var str = new String(s);
if (whitespace.indexOf(str.charAt(0)) != -1) {
var j=0, i = str.length;
while (j < i && whitespace.indexOf(str.charAt(j)) != -1){
j ;
}
str = str.substring (j, i);
}
return str;
}
//Remove the blank space on the right
function trimRight(s){
if(s == null) return " ";
var whitespace = new String(" tnr");
var str = new String(s);
if (whitespace.indexOf(str.charAt(str.length-1)) != -1){
var i = str.length - 1;
while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1){
i-- ;
}
str = str.substring(0, i 1);
}
return str;
}

When using, just call the trim function That’s it.
The following is the implementation method using regular expressions:
Copy the code The code is as follows:

< ;SCRIPT LANGUAGE="JavaScript">








Because of the editor problem, the spaces have been replaced in the above code, so Please pay attention to adding spaces and test.
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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.

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,

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.

How to enable and disable TRIM on Windows 11 How to enable and disable TRIM on Windows 11 Sep 29, 2023 pm 03:13 PM

Using an SSD drive leaves you with the constant worry of losing your data and being unable to recover it. However, Windows allows you to achieve optimal performance by executing TRIM commands that write only necessary data without having to manage old data blocks. To do this, you need to make sure your SSD supports TRIM and enable it in your operating system. How to check if TRIM is enabled? In most cases, TRIM functionality is enabled by default in modern SSDs. But to make sure this is checked out, you can run the command with administrative rights. Just open an elevated command prompt, run the fsutil behavioral query DisableDeleteNotify command and your SSD will be listed. 0 means enabled, 1 means disabled. like

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

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

PHP tutorial: Learn to use the trim function to remove Chinese spaces PHP tutorial: Learn to use the trim function to remove Chinese spaces Mar 27, 2024 am 11:00 AM

PHP Tutorial: Learn to use the trim function to remove Chinese spaces, you need specific code examples. In PHP development, you often encounter situations where you need to process strings, and one of the common requirements is to remove spaces at both ends of the string. When processing English strings, we can directly use PHP's built-in trim function to achieve this operation. However, when the string contains Chinese characters, the trim function may not be able to remove Chinese spaces normally. In this case, some special processing is required. method to achieve our needs. This article will introduce

How to use trim in Java How to use trim in Java May 02, 2023 pm 01:31 PM

1. Explain that trim() is the most commonly used method by Java developers to remove leading and trailing spaces. For the trim() method, a space character is any character with an ASCII value less than or equal to 32('U+0020')*. The trim() method returns a copy of the calling string object, but with all beginnings and ends removed. 2. Instance publicclassFunTester{publicstaticvoidmain(String[]args){Stringstring="onetwothree";System.out.prin

See all articles