# Special symbols in regular expressions and several methods of regular expressions of replace test search

• 2020-03-30 00:00:25
• OfStack

The body of a regular expression.
Special symbols are used in regular expressions. Below, I will briefly introduce the various symbols and their meanings and USES (note: "X above includes X") :

/ indicates that the text following is a special symbol. Example: "n" and "n" are identical. /n" is the same as the newline.
Alpha equals the beginning of the input.
\$matches the end of the input.
* is the same as the text before the symbol 0 times or more. Example: "zo*" is the same as "zoo" and "z".
If + is the same as the text before this symbol more than once, they are the same. Example: "zo*" is consistent with "zoo", but not with "z".
? If the text is the same 0 or 1 times before the symbol, they are the same. Example: "a? Ve?" It's consistent with "ve" for "never."
. Is consistent with all single characters except newline characters.
(regular expression) finds a consistent literal for the specified expression. If found, store it. Consistent parts can be obtained from
Found in the array obtained by the Match method.
X, |, y, x, and either side of y would be considered identical. Example: "(z|f)ood" is the same as "zood" and "food".
{n} n is an integer above 0. If it's the same thing n times, it's the same thing. Example :"o{2}" is different from "o" in "Bob"
To match the first two "o" in "foooood".
{n,} n is an integer above 0. It has to be the same at least n times.
{n,m} both integers. The degree from n to m is the same.
[xyz] is considered identical to any text in brackets.
[^xyz] as opposed to the above.
The range of [a-z] characters, from "a" to "z", is considered consistent.
[^a-z] is the opposite.
/b is the end of a word. Example: "er/b" is the same as the "er" of "never", but not the same as the "er" of "verb".
/B is the end of a non-word.
/d is the number.
/D is a non-number.
/s means space.
/S means non-space.
/w for all alphanumeric.
/W means not all alphanumeric.

I    (ignoring case) & provident;
G    (full text lookup of all     The pattern)
Gi    (full-text search, ignore case)
/num num should be given a positive number. Compare it to what has been stored. Example: "(.)/1" is the same as any two consecutive ones
Consistent writing.

How to define a text:
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 the exec method
Description: search within the specified text line.
Structure: regular expression. Exec (string).
Commentary: return of retrieval:
Null is not retrieved;
After the consistent results are retrieved;
Ex. :
The code snippet is as follows:
< Script>
Var s = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp'
Var r = new RegExp (' g ', 'I');
Var a = r.e xec (s);
< / script>

2 compile method:
Description: modify the internal form of the regular representation.
Structure: regular expression. Compile (' body ','g or I or ig').
Explanation: there's nothing to say.
Ex. :
The code snippet is as follows:
< Script>
Var s = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp'
Var r = new RegExp (' [a-z] ', 'g');
Var a = s.m atch (r);
R.com from running (' [a-z] ', 'g');
Var a = s.m atch (r);
< / script>

3 test method:
Description: as the name suggests, take a test.
Structure: regular expression.test(string).
Commentary: return:
True found;
Ex. :
The code snippet is as follows:
< Script>
Var re = / re/g;
Var MSG = 'return';
Var msg1 = 'goon';
< / script>

4 replace method:
Description: find the one and replace it.
Structure: string. Replace (regular expression, replace string).
Commentary: does not change with string, returns its copy.
Ex. :
The code snippet is as follows:
< Script>
Var s = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp'
Var r = new RegExp (' [a-z] ', 'g');
Var a = s.r eplace (/ [a-z] / g, 'a');
< / script>

5 match method:
Description: perform retrieval.
Structure: string. Match (regular expression).
Explanation: returns the sequence.
Ex. :
The code snippet is as follows:
< Script>
Var re = / re/g;
Var MSG = 'rererere';
Var msg1 = 'goon';
< / script>

6 split method:
Description: split the string.
Structure: string. Split (regular expression).
Explanation: returns the sequence.
Ex. :
The code snippet is as follows:
< Script>
Var s="hello this good world";
Var p = / / s/g;
Var a = s.s plit (p);
< / script>

7 search method:
Description: returns the position of a consistent string. (this is much more versatile than indexOf!)
Structure: string. Search (regular expression).
Explanation: return
Positive integers if found
Minus 1 if I don't find it
Ex. :
The code snippet is as follows:
< Script>
Var s="hello this good world";
Var p = / good/g;
Var a = s.s earch (p);
< / script>

Change the example of the replace method:
The code snippet is as follows:
< Script>
Var s = "hellOSCF";
Var r = new RegExp (" [a-z] ", "g");
S = s.r eplace (r, "a");
< / script>

Finally, his various attributes

1 lastIndex property:
Description: sets the starting position of the retrieval to obtain its value
Structure: regular expression. LastIndex (= value).
Explanation:
When lastIndex is greater than the length of the retrieved text, if the method test,exec is used, the execution fails.
The lastIndex property is set to 0.
When lastIndex is equal to the length of the retrieved text, it is the same if the body of the expression is empty.
Execution failed, reset to 0.
With the exception of the above,lastIndex is set as a pointer to the position of the last consistent text column.

2 the source attribute
Description: returns the body of the regular expression
Structure: regular expression. Source
Ex. :
The code snippet is as follows:
< Script>
Var s = / [a-z] {3} / W/s/g;
Var s1 = new RegExp (" [a-z] {3} / W ", "g");
< / script>

Next, I will write out several processing character functions:

1 forbidden number
The code snippet is as follows:
The function check (MSG) {
Var exe = / / d/g;
If (exe. Test (MSG)) return (0);
The else return (1)
}

Two letters only
The code snippet is as follows:
The function check (MSG) {
Var exe = / / W/g;
If (exe. Test (MSG)) return (0);
The else return (1);
}

3 no code
The code snippet is as follows:
The function check (MSG) {
Var exe = / < | (/ w/w) * > / g;
If (exe. Test (MSG)) return (0);
The else return (1);

Related articles: