Regular expression learning 1

JS regular expression validation rules

1. After learning about JS regular expressions, let’s first look at some examples of commonly used JS regular expressions:

The rules for mobile phone numbers generally start with 13, 14, 15, 16, 17, 18, and then the next 9 digits are any numbers from 0 to 9.

1. Mobile phone number verification:

var pattern = /^1(3|5|6|7|8)[0-9]{9}$/;
var str = "13688888888";
console.log(pattern.test(str));

2. Landline phone verification:

var re = /^0\d{2,3}-?\d{7,8}$/;
var str = "0371-769999";
console.log(re.test(str));

2. Based on these two regular expressions, we can see certain rules. So how to write a regular expression that we want to customize? First, you need to understand the meanings of these characters:

1.1 Simple escape characters:

For some characters that are difficult to write, add "/" in front. In fact, we are all familiar with these characters.

expression matchable Example
/r, /n Represents carriage return and line feed characters
/t Tabs
// Represents "/" itself
/^ Matches the ^ symbol itself
/$ Matches the $ sign itself The expression "/ d" matches "acc d" and matches "accd " When matching " a cc d422", the result is successful, the matched result is "$d", the matched position: starting position 3, ending position 4 (the default subscript starts from 0, the same below)
/. Matches the decimal point (.) itself The expression "/./ d " matches " acc . d " and matches "acc.d " When matching " a cc . d422", the result is success, the matched result is ".$d", the matched position: starting position 3, ending position 5
. match any character When the expression "./d{2}" matches "acc$d42", the result is successful. The matched result is "d42", and the matched position is: starting position 4, ending position 6
/d Any number, any one from 0 to 9 When the expression "/d{3}" matches "bb348aa", the result is successful, the matched result is "348", and the matched position: starting position 2, ending position 4

Expressions that match 'many characters'

Guess you like

Origin blog.csdn.net/t_1000poke/article/details/126683532