


Detailed explanation of JavaScript regular expressions and cascading effects
Regular expression (regular expression) is a string matching pattern, used to check whether a string contains a string of a specified pattern. Let’s share JavaScript_regular expressions and cascading effects with everyone through this article. Friends who are interested should take a look together
1. Regular expression (regular expression)
is a string matching pattern, used to check whether a string contains a string of the specified pattern.
2. Creation of regular expressions
##
var reg = /white/; var reg = new RegExp("white","g");
3. The regular expression modifier
g performs global matching (finding all matches instead of stopping after the first match is found).i Case-insensitive
m Multi-line matching
4. Regular expression symbols
square brackets : Square brackets are used to find characters within a range: [abc] Find any characters between square brackets.[^abc] Find any characters not between square brackets.
[0-9] Find any number from 0 to 9.
[a-z] Find any character from lowercase a to lowercase z.
[A-Z] Find any character from uppercase A to uppercase Z.
[A-z] Find any character from uppercase A to lowercase z.
[adgk] Find any character within the given set.
[^adgk] Find any character outside the given set.
(red|blue|green) Find any specified option.
^ matches the beginning of a string
$ Matches the end of the string
\s Any whitespace character
\S Any non-whitespace character
\d Matches a numeric character, equivalent to [0-9]
\D Except for digits Any character, equivalent to [^0-9]
\w Matches a number, underscore, or alphabetic character, equivalent to [A-Za-z0-9_]
\W Any non-single character character, equivalent In [^a-zA-z0-9_]
. Any character except newline character
{n,} Matches the previous item n times, or multiple times
{n,m} Matches the previous item at least n times, but not more than m times
* Matches the previous item 0 times or multiple times, equivalent to {0,}
+ Match the previous item one or more times, equivalent to {1,}
? Matches the previous item 0 or 1 times, which means the previous item is optional, equivalent to {0,1}
5, properties of the RegExp object
global Whether the RegExp object has the flag g, which declares whether the given regular expression performs global matching. ignoreCase Whether the RegExp object has flag i, which declares whether the given regular expression performs case-insensitive matching. multiline Whether the RegExp object has the flag m, which declares whether the given regular expression performs multi-line matching.6. Methods of RegExp object
1. exec searches for characters that match the regular expression, returns the found value, and Determine its locationexec()The exec() method retrieves a specified value from a string. The return value is the found value. If no match is found, null is returned. Example 1:var patt1=new RegExp("e"); document.write(patt1.exec("The best things in life are free")); 由于该字符串中存在字母 "e",以上代码的输出将是: e
var patt1=new RegExp("e"); document.write(patt1.test("The best things in life are free")); 由于该字符串中存在字母 "e",以上代码的输出将是: True
7. Analysis (email verification) var reg=/^\w+@\w+.[a -zA-Z]{2,3}(.[a-zA-Z]{2,3})?$/;
//Regular expression creation ^Start of string
$End of string
\wAny character letters and numbers, underscore
+ indicates that the previous character appears {1,}, one or more times.
@ Ordinary string
\w Any string ddd@123
. Any character except newline ddd@123.
[a-zA-Z] ddd@123.c ddd @123.n
{2,3} ddd@123.com ddd@123.net ddd@123.tv
(.[a-zA-Z]{2,3})? ddd@123. com.cn ddd@123.net
Password regularity:/^[a-zA-Z0-9]{4,10}$/
Birthday regularity:/^((19\d{2})|(200\d)) -(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/
Email regular: /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/
Postal code:/^\d{6}$/
Mobile phone number :/^1\d{10}$/
8. Methods of String object
match 找到一个或多个正则表达式的匹配
search 检索与正则表达式相匹配的值
replace 替换与正则表达式匹配的字符串
split 把字符串分割为字符串数组
9、select对象常用事件、方法和属性.
1、事件 onchange 当改变选项时调用的事件
2、方法 add() 向下拉列表中添加一个选项
示例:
var province=document.getElementById("selProvince").value; var city=document.getElementById("selCity"); city.options.length=0; switch(province){ case "河南省": city.add(new Option("郑州市","郑州市"),null); city.add(new Option("洛阳市","洛阳市"),null); break; …… }
3、属性:
options[] 返回包含下拉列表中的所有选项的一个数组
selectedIndex 设置或返回下拉列表中被选项目的索引号
length 返回下拉列表中的选项的数目
示例:
function get(){ var index=document.getElementById("fruit").selectedIndex; var len=document.getElementById("fruit").length; var show=document.getElementById("show"); show.innerHTML="被选选项的索引号为:"+index+"<br/>下拉列表选项数目为:"+len; }
4、Option对象常用属性:
text:设置或返回某个选项的纯文本值
value:设置或返回被送往服务器的值
10、数组常用的属性和方法。
属性 length 设置或返回数组中元素的数目
方法:
join( ) 把数组的所有元素放入一个字符串,通过一个的分隔符进行分隔
sort( ) 对数组的元素进行排序
****读取二维数组中的元素值:
var cityList = new Array(); cityList['河北省'] = ['邯郸市','石家庄市']; cityList['河南省'] = ['郑州市','洛阳市']; cityList['湖北省'] = ['武汉市','宜昌市']; for(var i in cityList){ document.getElementById("show").innerHTML+=i+"<br/>"; } for(var j in cityList){ for(var k in cityList[j]){ document.getElementById("show").innerHTML+=cityList[j][k]+" ”; } document.getElementById("show").innerHTML+="<br/>“; }
The above is the detailed content of Detailed explanation of JavaScript regular expressions and cascading effects. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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



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.

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

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

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

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

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

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
