Advanced JavaScript Chapter 11 (regular expressions) (end)

Understand the role of regular expressions

  1. Master the use of regular expressions

  • Create regular expressions

    • Regular expression composition

    • Boundary character

    • Character class

  • Regular test

Regular expression overview

1. What is a regular expression

Regular Expression is a pattern used to match character combinations in a string. In JavaScript, regular expressions are also objects.

Regular tables are usually used to retrieve and replace text that conforms to a certain pattern (rule), such as verification forms: the user name form can only enter English letters, numbers or underscores, and the nickname input box can enter Chinese (matching). In addition, regular expressions are often used to filter out some sensitive words in the page content (replacement), or get the specific part we want from the string (extraction), etc.

2. Characteristics of regular expressions

  1. Flexibility, logic and functionality are very strong.

  2. It can quickly achieve the complex control of string in a very simple way.

  3. For people who have just come into contact, it is more obscure and difficult to understand. For example: ^ \ w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w +)*$

Use of regular expressions in js

1. Creation of regular expression

In JavaScript, you can create a regular expression in two ways.

Method 1: create by calling the constructor of RegExp object

let regexp = new RegExp(/123/);
console.log(regexp);

  Method 2: create regular expressions with literal values

 let rg = /123/;

2. Test regular expressions

The test() regular object method is used to detect whether the string conforms to the rule. The object will return true or false, and its parameter is the test string.

let rg = /123/;
console.log(rg.test(123)); // Does 123 appear in matching characters   The result is true
console.log(rg.test('abc')); // Whether 123 appears in the matching character. If it does not appear, the result is false

Special characters in regular expressions

1. Composition of regular expressions

A regular expression can consist of simple characters, such as / abc /, or a combination of simple and special characters, such as / ab*c /. Special characters, also known as metacharacters, are special symbols with special significance in regular expressions, such as ^, $, + and so on.

Reference documents:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions

2. Boundary character

The boundary character (position character) in regular expression is used to indicate the position of the character. There are mainly two characters

3. Character class

Character class means that there are a series of characters to choose from, as long as you match one of them. All selectable characters are placed in square brackets.

1. [] square brackets

Indicates that there are a series of characters to choose from, as long as you match one of them

let rg = /[abc]/; // It returns true as long as it contains a, b or c
console.log(rg.test('andy')); // true
console.log(rg.test('baby')); // true
console.log(rg.test('color')); // true
console.log(rg.test('red')); // false
let rg1 = /^[abc]$/; // Choose one from three. Only the letters a, b or c return true
console.log(rg1.test('aa')); // false
console.log(rg1.test('a')); // true
console.log(rg1.test('b')); // true
console.log(rg1.test('c')); // true
console.log(rg1.test('abc')); // true
----------------------------------------------------------------------------------
let reg = /^[a-z]$/ // 26 English letters. Any letter returns true - indicating the range from a to z  
console.log(reg.test('a')); // true
console.log(reg.test('z')); // true
console.log(reg.test('A')); // false
-----------------------------------------------------------------------------------
// Character combination
let reg1 = /^[a-zA-Z0-9]$/; // 26 English letters (both uppercase and lowercase) any letter returns true  
------------------------------------------------------------------------------------
//The addition of ^ inside the inverted square brackets indicates negation. As long as the characters in the square brackets are included, false is returned.
let reg2 = /^[^a-zA-Z0-9]$/;
console.log(reg2.test('a')); // false
console.log(reg2.test('B')); // false
console.log(reg2.test(8)); // false
console.log(reg2.test('!')); // true

2. Quantifier

Quantifier is used to set the number of times a pattern appears.

  3. Bracketed summary

1. Curly bracket quantifier. It indicates the number of repetitions

2. Bracketed character set. Matches any character in parentheses

3. Parentheses indicate priority

4. Predefined classes

Predefined classes are shorthand for some common patterns

4. Regular replace

The replace() method can implement the replace string operation, and the parameter used to replace can be a string or a regular expression.

let str = 'andy and red';
let newStr = str.replace('andy', 'baby');
console.log(newStr)//baby and red
// andy equivalent here can be written in a regular expression
let newStr2 = str.replace(/andy/, 'baby');
console.log(newStr2)//baby and red
// replace all
let str = 'abcabc'
let nStr = str.replace(/a/,'ha-ha')
console.log(nStr) //Ha ha bcabc
// Replace all g
let nStr = str.replace(/a/a,'ha-ha')
console.log(nStr) // Ha ha bc ha ha bc
// Ignore case i
let str = 'aAbcAba';
let newStr = str.replace(/a/gi,'ha-ha') // "Ha ha ha bc ha ha b ha ha"

To readers:

      The above is the whole content of JavaScript advanced, if you have the patience to start from the beginning Getting started with JavaScript After learning this, congratulations. Your JavaScript foundation is very solid. You can consider learning jq or framework,

Next, we will enter jq's study. Come on!!!

Previous chapter: Advanced JavaScript Chapter 10 (recursion)

Tags: Javascript regex perl

Posted on Sun, 05 Sep 2021 02:55:54 -0400 by moneytree