The body of a regular expression.
Special symbols are used in regular expressions. Below I will briefly introduce the various symbols, their meanings and usage (note: "X and above include X"):
/ indicates that the text following it is a special symbol. Example: "n" and "n" are consistent. "/n" is the same as the newline character.
^ coincides with the beginning of the input.
$ matches the end of the input.
* If the characters before this symbol are the same 0 or more times, they are consistent. Example: "zo*" is consistent with "zoo" and "z".
+ If the characters before this symbol are the same more than once, they are consistent. Example: "zo*" is consistent with "zoo", but inconsistent with "z".
? If the characters before this symbol are the same 0 or 1 times, they are consistent. Example: "a?ve?" is consistent with "ve" in "never".
. Consistent with all single literals except newlines.
(Regular Expression) Find matching text using the specified expression. If found, save it. The consistent part can be obtained from
Found in the array obtained by the Match method.
If any one of x|yx and y is the same, it will be considered consistent. Example: "(z|f)ood" is consistent with "zood" and "food".
{n} n is an integer above 0. If it is the same as the previous text n times, the two are consistent. Example: The "o" in "o{2}" and "Bob" are different
Consistent with the first two "o"s in "foooood".
{n,} n is an integer above 0. It must be consistent with the previous text at least n times.
{n,m} Both integers. The degrees in the range n to m are consistent.
[xyz] is considered consistent if it is the same as any of the characters in square brackets.
[^xyz] is the opposite of the above.
[az] The range of characters from "a" to "z" are considered consistent.
[^az] Contrary to the above.
/b indicates the end of the word. Example: "er/b" is consistent with the "er" of "never", but inconsistent with the "er" of "verb".
/B indicates the end of a non-word.
/d represents a number.
/D means not a number.
/s means space.
/S means non-space.
/w means all alphanumeric characters.
/W means not all alphanumeric characters.
i (ignore case)
g (Find all occurrences of pattern in the full text)
gi (full text search, ignore case)
/num num should be assigned a positive number. Compare with the already stored part. Example: "(.)/1" is the same as any two consecutive ones
The text is consistent.
2. How to define the main text of a sentence:
Method 1: Write directly
var s=/regular expression/i or g or ig
Method 2: Create an object instance:
var s=new RegExp(regular expression, i or g or ig)
Three methods related to regular expressions:
1 exec method
Description: Search within a specified line of text.
Structure: regular expression.exec(string).
Explanation: The search returns:
null not retrieved;
After consistent results are retrieved;
example:
The code snippet is as follows:
<script>
var s='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp'
var r=new RegExp('g','i');
var a=r.exec(s);
alert(a);
</script>
2 compile method:
Description: Modifies the internal form of regular representation.
Structure: Regular expression.compile('text','g or i or ig').
Commentary: Nothing much to say.
example:
The code snippet is as follows:
<script>
var s='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp'
var r=new RegExp('[AZ]','g');
var a=s.match(r);
alert(a);
r.compile('[az]','g');
var a=s.match(r);
alert(a);
</script>
3 test method:
Description: As the name suggests, take quizzes.
Structure: regular expression.test(string).
Commentary: Return:
false not found;
true found;
example:
The code snippet is as follows:
<script>
var re=/re/g;
var msg='return';
var msg1='goon';
alert(re.test(msg));
alert(re.test(msg1));
</script>
4 replace method:
Description: Find the consistent one and replace it.
Structure: string.replace (regular expression, replacement string).
Explanation: The string is unchanged and its copy is returned.
example:
The code snippet is as follows:
<script>
var s='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp'
var r=new RegExp('[AZ]','g');
var a=s.replace(/[az]/g,'A');
alert(a);
</script>
5 match method:
Description: Perform a search.
Structure: string.match (regular expression).
Explanation: Return the sequence.
example:
The code snippet is as follows:
<script>
var re=/re/g;
var msg='rererere';
var msg1='goon';
alert(msg.match(re));
alert(msg1.match(re));
</script>
6 split method:
Description: Split a string.
Structure: String.split (regular expression).
Explanation: Return the sequence.
example:
The code snippet is as follows:
<script>
var s="hello this good world";
var p=//s/g;
var a=s.split(p);
alert(a);
</script>
7 search method:
Description: Returns the position of a consistent string. (This is much more versatile than indexOf!)
Structure: string.search(regular expression).
Commentary: Return
Positive integer if found
-1 if not found
example:
The code snippet is as follows:
<script>
var s="hello this good world";
var p=/good/g;
var a=s.search(p);
alert(a);
</script>
Change the example of the replace method:
The code snippet is as follows:
<script>
var s="hellOSCF";
var r=new RegExp("[AZ]","g");
s=s.replace(r,"a");
alert(s)
</script>
Finally, there are his various attributes
1 lastIndex attribute:
Description: Set the starting position of the search and get its value
Structure: regular expression.lastIndex(=value).
Commentary:
When lastIndex is greater than the length of the retrieved text, if executed using the test or exec methods, the execution will fail.
The lastIndex property is set to 0.
When lastIndex is equal to the search text length, if the expression body is empty, it is consistent. In other cases,
Execution failed and reset to 0.
In cases other than the above, lastIndex will be set to the position pointer of the last consistent text string.
2 source attribute
Description: Returns the text of the regular expression
Structure: regular expression.source
example:
The code snippet is as follows:
<script>
var s=/[az]{3}/W/s/g;
var s1=new RegExp("[az]{3}/W","g");
alert(s.source);
alert(s1.source);
</script>
Below I will write out several character processing functions:
1 Numbers are strictly prohibited
The code snippet is as follows:
function check(msg){
var exe=//d/g;
if(exe.test(msg))return(0);
else return(1)
}
2 letters only
The code snippet is as follows:
function check(msg){
var exe=//W/g;
if(exe.test(msg))return(0);
else return(1);
}
3 Strictly prohibited codes
The code snippet is as follows:
function check(msg){
var exe=/<(/w|/W)*>/g;
if(exe.test(msg))return(0);
else return(1);