Chapter 2 Regular Expressions

Chapter 2 Regular Expressions

1. Learning Objectives

  • Master the role of regular expressions
  • Master the syntax of regular expressions
  • Learn about common regular expressions

2. Content explanation

2.1 The concept of regular expressions

A regular expression is a logical formula for character string operations. It is to use some specific characters defined in advance and the combination of these specific characters to form a "rule string". This "rule string" is used to express the character string A filtering logic for strings. In our own words: a regular expression is a formula used to check whether a string meets certain rules

2.2 Purpose of regular expressions

So regular expressions have three main uses:

  • Pattern verification: Detect whether a string conforms to the rules, such as detecting whether the mobile phone number, ID number, etc. conform to the specification
  • Matching read: Read out the part of the target string that meets the rules , for example, read out the email address in the entire text
  • Match replacement: Replace the part of the target string that meets the criteria with other strings, for example, replace "hello" in the entire text with "haha"

2.3 Syntax of regular expressions

2.3.1 Create a regular expression object

  • Object form: var reg = new RegExp("正则表达式")when there is "/" in the regular expression, then use this
  • Direct quantity form: var reg = /正则表达式/generally use this declaration method

2.3.2 Introductory case of regular expressions

2.3.2.1 Mode verification: Check if the string contains the letter 'o'

Note : here is to use the regular expression object to call the method.

// 创建一个最简单的正则表达式对象
var reg = /o/;

// 创建一个字符串对象作为目标字符串
var str = 'Hello World!';

// 调用正则表达式对象的test()方法验证目标字符串是否满足我们指定的这个模式,返回结果true
console.log("字符串中是否包含'o'="+reg.test(str));

2.3.2.2 Matching read: read all 'o' in the string

//匹配读取: 读取一个字符串中的所有'l'字母
// g表示全文查找,如果不使用g那么就只能查找到第一个匹配的内容
//1. 编写一个正则表达式
var reg2 = /l/g
//2. 使用正则表达式去读取字符串
var arr = str.match(reg2);
console.log(arr)

2.3.2.3 Match replacement: Replace the first 'o' in the string with '@'

var newStr = str.replace(reg,'@');
// 只有第一个o被替换了,说明我们这个正则表达式只能匹配第一个满足的字符串
console.log("str.replace(reg)="+newStr);//Hell@ World!
// 原字符串并没有变化,只是返回了一个新字符串
console.log("str="+str);//str=Hello World!

2.3.3 Matching mode of regular expression

2.3.3.1 Full Text Search

If g is not used to modify the regular expression object, only the first match will be returned when the regular expression is used to search; after g is used, all matches will be returned.

// 目标字符串
var targetStr = 'Hello World!';

// 没有使用全局匹配的正则表达式
var reg = /[A-Z]/;
// 获取全部匹配
var resultArr = targetStr.match(reg);
// 数组长度为1
console.log("resultArr.length="+resultArr.length);

// 遍历数组,发现只能得到'H'
for(var i = 0; i < resultArr.length; i++){
    
    
    console.log("resultArr["+i+"]="+resultArr[i]);
}

Comparison code:

// 目标字符串
var targetStr = 'Hello World!';

// 使用了全局匹配的正则表达式
var reg = /[A-Z]/g;
// 获取全部匹配
var resultArr = targetStr.match(reg);
// 数组长度为2
console.log("resultArr.length="+resultArr.length);

// 遍历数组,发现可以获取到“H”和“W”
for(var i = 0; i < resultArr.length; i++){
    
    
    console.log("resultArr["+i+"]="+resultArr[i]);
}

2.3.3.2 Ignore case

//目标字符串
var targetStr = 'Hello WORLD!';

//没有使用忽略大小写的正则表达式
var reg = /o/g;
//获取全部匹配
var resultArr = targetStr.match(reg);
//数组长度为1
console.log("resultArr.length="+resultArr.length);
//遍历数组,仅得到'o'
for(var i = 0; i < resultArr.length; i++){
    
    
    console.log("resultArr["+i+"]="+resultArr[i]);
}

Comparison code:

//目标字符串
var targetStr = 'Hello WORLD!';

//使用了忽略大小写的正则表达式
var reg = /o/gi;
//获取全部匹配
var resultArr = targetStr.match(reg);
//数组长度为2
console.log("resultArr.length="+resultArr.length);
//遍历数组,得到'o'和'O'
for(var i = 0; i < resultArr.length; i++){
    
    
    console.log("resultArr["+i+"]="+resultArr[i]);
}

2.3.3.3 Multi-line search

Without multi-line search mode, the target string will be treated as one line regardless of whether there is a newline character or not.

//目标字符串1
var targetStr01 = 'Hello\nWorld!';
//目标字符串2
var targetStr02 = 'Hello';

//匹配以'Hello'结尾的正则表达式,没有使用多行匹配
var reg = /Hello$/;
console.log(reg.test(targetStr01));//false

console.log(reg.test(targetStr02));//true

Comparison code:

//目标字符串1
var targetStr01 = 'Hello\nWorld!';
//目标字符串2
var targetStr02 = 'Hello';

//匹配以'Hello'结尾的正则表达式,使用了多行匹配
var reg = /Hello$/m;
console.log(reg.test(targetStr01));//true

console.log(reg.test(targetStr02));//true

2.3.4 Metacharacters

Characters assigned special meanings in regular expressions cannot be directly used as ordinary characters. If you want to match the metacharacter itself, you need to escape the metacharacter by adding "\" in front of the metacharacter, for example: ^

2.3.4.1 Commonly used metacharacters

the code illustrate
. Matches any character except a newline character.
\w Match letters or numbers or underscores are equivalent to [a-zA-Z0-9_]
\W Matches any non-word character. Equivalent to A-Za-z0-9_
\s Matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [\f\n\r\t\v].
\S Matches any non-whitespace character. Equivalent to \f\n\r\t\v.
\d Match numbers. Equivalent to [0-9].
\D Matches a non-numeric character. Equivalent to 0-9
\b matches the start or end of a word
^ Match the beginning of the string, but use it in [] to negate
$ matches the end of the string

2.3.4.2 Example 1

var str = 'one two three four';
// 匹配全部空格
var reg = /\s/g;
// 将空格替换为@
var newStr = str.replace(reg,'@'); // one@two@three@four
console.log("newStr="+newStr);

2.3.4.3 Example 2

var str = '今年是2014年';
// 匹配至少一个数字
var reg = /\d+/g;
str = str.replace(reg,'abcd');
console.log('str='+str); // 今年是abcd年

2.3.4.4 Example 3

var str01 = 'I love Java';
var str02 = 'Java love me';
// 匹配以Java开头
var reg = /^Java/g;
console.log('reg.test(str01)='+reg.test(str01)); // flase
console.log("<br />");
console.log('reg.test(str02)='+reg.test(str02)); // true

2.3.4.5 Example 4

var str01 = 'I love Java';
var str02 = 'Java love me';
// 匹配以Java结尾
var reg = /Java$/g;
console.log('reg.test(str01)='+reg.test(str01)); // true
console.log("<br />");
console.log('reg.test(str02)='+reg.test(str02)); // flase

2.3.5 Character Sets

grammatical format example illustrate
[character list] Regular expression: [abc] Meaning: The target string contains any character in abc Target string: plain Does it match: Yes Reason: "a" in plain is in the list "abc" Any character in the target string that appears in the character list is considered a match.
[^char list] [^abc] Meaning: The target string contains any character other than abc Target string: plain Whether it matches: Yes Reason: plain contains "p", "l", "i", "n" Matches any character not included in the character list.
[character range] Regular expression: [az] Meaning: character list of all lowercase English characters Regular expression: [AZ] Meaning: character list of all uppercase English characters Matches any character in the specified range.
var str01 = 'Hello World';
var str02 = 'I am Tom';
//匹配abc中的任何一个
var reg = /[abc]/g;
console.log('reg.test(str01)='+reg.test(str01));//flase
console.log('reg.test(str02)='+reg.test(str02));//true

2.3.6 Occurrences

the code illustrate
* zero or more occurrences
+ occurs one or more times
? zero or one occurrence
{n} appears n times
{n,} appear n or more times
{n,m} occurs n to m times
console.log("/[a]{3}/.test('aa')="+/[a]{3}/g.test('aa')); // flase
console.log("/[a]{3}/.test('aaa')="+/[a]{3}/g.test('aaa')); // true
console.log("/[a]{3}/.test('aaaa')="+/[a]{3}/g.test('aaaa')); // true

2.3.7 Expressing "or" in regular expressions

Use symbols: |

// 目标字符串
var str01 = 'Hello World!';
var str02 = 'I love Java';
// 匹配'World'或'Java'
var reg = /World|Java/g;
console.log("str01.match(reg)[0]="+str01.match(reg)[0]);//World
console.log("str02.match(reg)[0]="+str02.match(reg)[0]);//Java

2.4 Common regular expressions

need regular expression
username / 1 [a-zA-Z_-0-9]{5.9}$/
password /2{6,12}$/
leading and trailing spaces /^\s+|\s+$/g
E-mail /3+@([a-zA-Z0-9-]+[.]{1})+[a-zA-Z]+$/

  1. a-zA-Z_ ↩︎

  2. a-zA-Z0-9_-@#&* ↩︎

  3. a-zA-Z0-9_.- ↩︎

Guess you like

Origin blog.csdn.net/apple_67445472/article/details/131739397