


Introduction to the use of split function in JavaScript from shallow to deep
Dec 05, 2017 pm 03:43 PM相信很多小伙伴都知道JavaScript中split就是將一字符串以特定的字符分割成數(shù)組,數(shù)組一般是字符串處理比較常用的處理方法,但是沒有接觸過JavaScript的朋友來說,可能就有點不明白了,今天我們就帶大家介紹下JavaScript中split函數(shù)由淺到深的使用!
split定義和用法
split() 方法用于把一個字符串分割成字符串數(shù)組。
語法
stringObject.split(separator,howmany)
參數(shù) 描述
separator 必需。字符串或正則表達式,從該參數(shù)指定的地方分割 stringObject。
howmany 可選。該參數(shù)可指定返回的數(shù)組的最大長度。如果設(shè)置了該參數(shù),返回的子串不會多于這個參數(shù)指定的數(shù)組。如果沒有設(shè)置該參數(shù),整個字符串都會被分割,不考慮它的長度。
返回值
一個字符串數(shù)組。該數(shù)組是通過在 separator 指定的邊界處將字符串 stringObject 分割成子串創(chuàng)建的。返回的數(shù)組中的字串不包括 separator 自身。
但是,如果 separator 是包含子表達式的正則表達式,那么返回的數(shù)組中包括與這些子表達式匹配的字串(但不包括與整個正則表達式匹配的文本)。
下面直接舉例給大家看看
<script language="javascript"> str="2,2,3,5,6,6"; //這是一字符串 var strs= new Array(); //定義一數(shù)組 strs=str.split(","); //字符分割 for (i=0;i<strs.length ;i++ ) { document.write(strs[i]+"<br>"); //分割后的字符輸出 } </script>
split進階技巧(特殊字符串的處理):
在java.lang包中有String.split()方法,返回是一個數(shù)組
我在應(yīng)用中用到一些,給大家總結(jié)一下,僅供大家參考:
1、如果用“.”作為分隔的話,必須是如下寫法:String.split("\\."),這樣才能正確的分隔開,不能用String.split(".");
2、如果用“|”作為分隔的話,必須是如下寫法:String.split("\\|"),這樣才能正確的分隔開,不能用String.split("|");
3、如果用“\”作為分隔的話,必須是如下寫法:String.split(\\\),這樣才能正確的分隔開,不能用String.split("\");
“.”,“|”和“\”都是轉(zhuǎn)義字符,必須得加"\\";
3、如果在一個字符串中有多個分隔符,可以用“|”作為連字符,比如:“acount=? and uu =? or n=?”,把三個都分隔出來,可以用String.split("and|or");
例1:
如果想在串中使用"\"字符,則也需要轉(zhuǎn)義.例如首先要表達"aaaa\bbbb"這個串就應(yīng)該用"aaaa\\bbbb",如果要分隔就應(yīng)該這樣才能得到正確結(jié)果:
String[] aa = "aaa\\bbb\\bccc".split(\\\\);
例2:
總結(jié):
本文從簡到深的介紹了JavaScript中split函數(shù)的使用,通過對本文的學習相信很多小伙伴對此都有了一定的了解和認識,希望對你的工作有所幫助!
相關(guān)推薦:
php中explode()函數(shù)與split()函數(shù)的區(qū)別
js?split?的用法和定義 js?split分割字符串成數(shù)組的實例代碼
JavaScript使用split將字符串分割為數(shù)組
The above is the detailed content of Introduction to the use of split function in JavaScript from shallow to deep. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ??and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
