JS study notes eleven-regular expressions

One, regular expression

Regular expressions are used to express the rules of some strings. The computer can check whether a string conforms to the rules according to the regular expressions, or extract the content that conforms to the rules in the string.

1. Create regular expression objects

Syntax:var 变量=new RegExp("正则表达式",“匹配模式”) ;

var reg=new RegExp();

In the constructor, you can pass a matching mode as the second parameter, which can be: i ignore case g global matching mode

2. Check whether the string meets the rules of regular expressions

(1) The test() method can be used to check whether a string conforms to the rules of regular expressions, if it conforms, it returns true, otherwise it returns false.

var reg=new RegExp("a");
var str="a";
var result=reg.test(str);
console.log(result);

(2) var reg=new RegExp("a"); This regular expression can be used to check whether a string contains a. It is strictly case-sensitive, and A is not. You can use the parameter i of the matching pattern to ignore the case.

3. Use literals to create regular expressions

Syntax: var variable=/regular expression/matching pattern It
is easier to create using literal methods, and it is more flexible to create using constructors.
(1) Create a regular expression to check whether there is a or b in a string, and use | to indicate the meaning of or

var reg=/a|b/;//有a或者有b的正则表达式

(2) Create a regular expression to check whether
the content in the letter [] in a string is also an OR relationship: [ab]==a|b

[az] any lowercase letter a to z
[AZ] any uppercase letter A to Z
[Az] any letter
[^ ab] except ab
[0-9] any number
[^0-9] except numbers

(3) Check whether a string contains abc or adc or aecreg=/a[bde]c/

Two, string and regular expression related methods

1. Split() method:

(1) A string can be split into an array
(2) A regular expression can be passed as a parameter in the method, so that the method will split the string according to the regular expression.
(3) Even if this method does not specify a global match, it will all split

var str="1a2b3d4h5l";
var result=str.split(/[a-z]/);//根据字母拆分字符串
console.log(result);//1,2,3,4,5

2.search() method

(1) You can search for the specified content in the string.
(2) If the specified content is searched, it will return the index of the first occurrence, if it is not searched, it will return -1.
(3) A regular expression can be accepted as a parameter, and then the string will be retrieved according to the regular expression .
(4) serach() will only find the first one, even if the global match is set, it is useless
Insert picture description here

3.match() method

(1) According to regular expressions, the qualified content can be extracted from a string.
(2) By default, our math will only find the first content that meets the requirements. After finding it, it will stop searching. We can set the regular expression to the global matching mode, so that all the content will be matched.
(3) Multiple matching modes can be set for a regular expression, and the order does not matter
(4) match() will encapsulate the matched content into an array and return, even if only one result is found

var str="1a2b3d4h5l";
var result=str.match(/[A-z]/g);//全局查找
var result=str.match(/[A-z]/ig);//全局查找,忽略大小写
console.log(result);//a,b,d,h,l

4.replace() method

(1) The specified content in the string can be replaced with new content
(2) Parameters:
1. The content to be replaced can accept a regular expression as a parameter
2. New content
(3) By default, only the first one will be replaced

var str="1a2b3d4h5l";
result=str.replace("/a-z/ig","@_@");//替换所有的字母

Three, regular expression syntax

1. Quantifier

(1) The number of occurrences of a content can be set by the quantifier
(2)/x{y}/ means to find y consecutive x together.
(3) The quantifier has an effect on the preceding content
(4) {m,n}: appears m to n times
(5) {m,}: appears more than m times
(6) + at least one, equivalent to {1, }
(7) * 0 or more, equivalent to {0,}
(8)? 0 or 1, equivalent to {0,1}
(9)^a means beginning with a
(10) a$ means ending with a
(11) If ^ is used at the same time in a regular expression, the string must be fully qualified Regular expressions. / a requires that the string must fully conform to the regular expression. /^aThen to seek the word character string must be completely full character fit n the Table of formula . /a /
(12) Start with a or end with a/^a|a$/

2. Mobile phone number

  1. Start with 1^1
  2. The second digit 3-9 any number [3-9]
  3. 9 arbitrary numbers after three digits [0-9]{9}$
var phoneReg=new RegExp();
		phoneReg=/^1[3-9][0-9]{9}$/;
		var phoneStr="15927212715";
		console.log(phoneReg.test(phoneStr));

3.Check whether a string contains.

(1). Represent any character
(2) Use \ as an escape character in regular expressions: \. to represent.
(3) \\ represents
(4) Note: When using the constructor to create a regular expression, due to its The parameter is a string, and \ is the escape character in the string. If you want to use \, you need to use \ instead

\w means any letter, number, underscore [A-z0-9_]
\W except for any letter, number, underscore [^A-z0-9_]
\d any number [0-9]
\D except for numbers [^0- 9]
\s space
\S except space
\b word boundary
\D except word boundary

(5) Receiving a user’s input // Remove the spaces
before and after the characters
// To remove the spaces is to use "" to replace the spaces

var str="    hello     ";
console.log(str);
str=str.replace(/\s/g,"");//去除掉字符中前后的空格
str=str.replace(/^\s*/g,"");//去除开头的空格
str=str.replace(/\s*$/g,"");//去除结尾的空格
str=str.replace(/^\s*|\s*$/g,"");//去除开头和结尾的空格

4. Email

(1) Composition: Any alphanumeric underscore. Any alphanumeric underscore @ Any alphanumeric. Any letter (2-5 digits). Any letter (2-5 digits)
(2) /^\w{3,} (.\ w+)*@ [A-z0-9]+ (.[Az]{2,5}){1,2}$/

Guess you like

Origin blog.csdn.net/weixin_45636381/article/details/113095242