


Summary of javascript regular expression definition (grammar)_javascript skills
This article talks about javascript regular expression definition (grammar). Share it with everyone for your reference, the details are as follows:
There are two ways to define regular expressions: one is to call RegExp() directly, and the second is to define it directly with literals, that is, var re = /regular rule/;
Both definition methods essentially call the RegExp() method
When calling the same regular code, the behavior in ECMAScript3 and ECMAScript5 is completely different
function reg(){ var re = /\sjavascript/; return re; }
分別在ECMAScript3和ECMAScript5中呼叫reg()方法多次
在ECMAScript3中,調用的是同一個RegExp對象,在ECMAScript5中,調用的是不同的RegExp對象 因為在EXCMAScript5中每執行一次,就生成一個新的RegExp對象
所以在ECMAScript3中會造成程式的隱患,因為只要在一個地方對這個物件進行修改的話,所有呼叫到這個物件的地方都會改變。
1.直接量字
在正規中一般都會直接匹配字符,如
/javascript/
會直接匹配字符javascript
也支援非字母的字元匹配,如:
o NUL字元(u0000)
t 製表符(u0009)
n 換行符號(u000A)
v 垂直製表符(u000B)
f 換頁符(u000C)
r 回車符(u000D)
xnn 由十六進制數nn指定的拉丁字符,例如,x0A等價於
uxxxx 由十六進位數xxxx指定的Unicode字符,例如u0009等價於
cX 控製字元^X,例如,cJ等價於換行符n
在正規表示式中,還有一些有特殊意義的標點符號,他們需要''來轉義
^$.*+?=!:|/()[]{}
2.字元類別
[...] 方括號內的任一字元
[^...] 不在方括號內的任一字元
. 任一字元
w 任何ASCII字元組成的單字,等價於[a-zA-Z0-9]
W 任何不適ASCII字元組成的單詞,等價於[^a-zA-Z0-9]
s 任何Unicode空白符
S 任何非Unicode空白符的字符,並注意w和S不一樣
d 任何ASCII數值,等價於[0-9]
D 除了ASCII數字之外的任何字符,等價於[^0-9]
[b] 退格直接量(特例)
3.重複(次數)
? 0或1次
+ 1次或多次
* 任次
{n} n次
{m,n} 最少m次,最多n次
{n,} n次或n次以上
正規預設是貪婪匹配的
如[a+b+] 若要符合aaabb,且它不會符合ab和aab等,只會符合aaabb
[a+?b+?] 這個會符合aaab 為什麼會產生這個差異呢?
答:+?是讓正則非貪婪匹配,那麼b這裡只會匹配一個b,那為什麼a會匹配3個呢?這是因為正規表示式的模式比對總是會尋找字串中第一個可能符合的位置。
4.選項|分組|引用
| 用於分隔可供選擇的字符,如[ab|cd],他既可以匹配ab也可以匹配cd,注意:選擇項的嘗試匹配次序是左→右,因此[a|ab],當a匹配通過了之後,就不匹配ab了,就算ab是更好的匹配
() 1.單獨的項當成子表達式 /java(script)?/ 可將javascript與java 即圓括號部分形成子的表達式,可對子表達式執行| * ?
2.完整的模式中定義子模式 後面的可以引用前面圓括號起來的表達式 /(['"])[a-z]1/ 1引用的是第一個圓括號裡的表達式,因此引用了['"]3.後部引用前面的子表達式
注意: /['"][a-z]['"]/這個正規的意思是 單引號或雙引號加上一個小寫字母加則加上一個單引號或雙引號,前後的單雙引號不是匹配的如果你要匹配可以這麼寫[(['"])[a-z]1]
加數字 可以引用前面圓括號中的表達式
5.制定匹配位置(錨點)
^ 符合字串的開頭,在多行檢索中,符合一行的開頭$ 符合字串的結尾,在多行檢索中,符合一行的結尾
b 符合一個單字的邊界,簡言之,就是位於字元w和W之間的位置,或位於字元w和字串的開頭或結尾之間的位置
B 符合非單字邊界的位置
(?=p) 零寬正向先行斷言,要求接下來的字元都與p匹配,但不能包括那些匹配p的字元
(?!p) 零寬負向先行斷言,要求接下來的字元不與p匹配
6.修飾符
寫在正規表示式字面量//右邊的
i 執行不區分大小寫的符合
g 執行一個全域匹配,簡言之,即找到所有的匹配,而不是在找到第一個之後就停止
m 多行配對模式,^符合一行的開頭與字串的開頭,$相符行的結束與字串的結束 /java$/m 可以符合 javanfunc
注意:當正規表示式是全域的時候,每次exec() 和test()的時候都會把目前設定的lastIndex設為目前的位置,再次執行的時候就會從lastIndex的位置開始執行,因此最好每次執行的時候lastIndex設定為0
希望本文所述對大家JavaScript程式設計有所幫助。

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

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



PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

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

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.

PHP is a widely used programming language, especially popular in the field of web development. In the process of web development, we often encounter the need to filter and verify text input by users, among which character filtering is a very important operation. This article will introduce how to use regular expressions in PHP to implement Chinese character filtering, and give specific code examples. First of all, we need to clarify that the Unicode range of Chinese characters is from u4e00 to u9fa5, that is, all Chinese characters are in this range.

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
