Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

A regular expression of JavaScript


May 30, 2021 Article blog


Table of contents


1. What is a regular expression

Regular Expression is an object that describes character patterns, ECMAcript's RegExp class represents regular expressions, and Both String and RegExp define functions that use powerful pattern matching and text retrieval and replacement with regular expressions

2. How regular expressions are created

The first way to create

Var Box = new regexp ('box'); // The first is the parameter string var box = new regexp ('Box', 'Ig') // The second is an optional mode modifier

The second way to create

var box=/box/; //直接用反斜杠 <span style="white-space:pre"> </span>var box=/box/ig; //在第2个反斜杠后面加上模式修饰符

 A regular expression of JavaScript1

3. Test the regular expression

The RegExp object consists of two methods: test() and exec() for test string matching.

The test() method finds in the string whether the specified regular expression exists and returns the Boolean value, and if so, true. I f it does not exist, false is returned.

exec() is also used to find regular expressions in strings, and if the exec() method performs successfully, it returns coherent information that includes the lookup string and, if it fails, null.

/ * Use the Test method of the New operator * / var pattern = new regexp ('Box', 'I'); // Create a regular expression, do not distinguish the case VAR str = "this is a box!"; // Create a string Alert (pattern.test (str)); // via Test () method verification is a match / * Test method using a literal mode * / var pattern = / box / i; // Create a regular expression, do not distinguish the case Var str = "this is a box!"; Alert (Pattern.Test (STR)); / * Return to match in the match * / var pattern = / box / i; var str= "This is a box!"; Alert (pattern.exec (str)); // Match the return array, otherwise returns NULL

 A regular expression of JavaScript2

4. Get control

Regular expression metacharacters are characters that include special meanings. They have a special feature that controls how the pattern is matched, and the metacharacters behind the backslash lose their special meaning.

Greed and idleness

The syntax distinction between greedy mode and idle mode lies in whether there is a question mark behind the repeated qualifying modifier, and if so, the idle mode, otherwise it is greedy mode.

Greedy measure is to eat all the characters first, and then spit it out one by one until the match is successful.

Lazy word, is from the beginning 1 character 1 character to eat, until the match is successful. That is, greed is a gradual forward match, while idleness is a gradual match backward.

example:

<span style="white-space:pre"> </span>
VAR pattern = / [a-z] +? / //?Close the greed match, only replace the first one
<span style="white-space:pre"> </span>
var str='ajfifdjnfasdfdasdgh';
<span style="white-space:pre"> </span>
var result=str.replace(pattern,'xxx');
<span style="white-space:pre"> </span>
Alert (result); // Return xxxjfifdjnfasdfdasdgh

<span style="white-space:pre"> </span>
<pre name="code" class="javascript">
<span style="white-space:pre"></span
> var pattern = / 8 (. +?) 8 / g; // Stop greed, open global
<span style="white-space:pre"> </span>
var str='this is 8abc8, that is 8abc8, there is 8abc8';
<span style="white-space:pre"> </span>
var result=str.replace(pattern,'<strong>$1</strong>');
<span style="white-space:pre"> </span>document.write(result); //返回this is abc, that is abc, there is abc

summary:

Personally, by validating on the client side with regular expressions, you can reduce the pressure on the server side before verifying it

The following courses are available for learning practice
Regular expression microserancy

A brief analysis of regular expressions in a video lesson

A concise reference to regular expressions