Summary of several methods of special symbols and regular expressions in regular expressions (Replace,test,search) _javascript techniques

Source: Internet
Author: User
Tags alphanumeric characters

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

/indicates that the following text is a special symbol. Example: "N" and "n" are consistent. "/n" is consistent with line breaks.
^ and the beginning of the input are consistent.
The $ is the same as the end of the input.
* The same words as before this symbol 0 times, the two agree. Examples: "zo*" and "Zoo", "Z" are consistent.
+ the same words 1 times before this symbol, the two agree. Example: "zo*" and "zoo" are consistent, but inconsistent with "Z".
? The same words that preceded the symbol 0 or 1 times are the same. Example: "A?ve?" and "Never" of "VE" consistent.
. Consistent with all the single text except for line breaks.
(regular expression) to find consistent text in the expression you specify. If you find it, save it. The consistent part can be from
Found in the array obtained by the match method.
X|y x and Y will be considered consistent as any of the same parties. Example: "(z|f) Ood" and "Zood", "food" are consistent.
{n} n is an integer above 0. The same words n times as before, the two agree. Example: "O" in "o{2}" and "Bob" is different
, consistent with the first two "O" in "Foooood".
{N,} n is an integer above 0. Consistent with the previous text at least n times.
{n,m} both integers. The number of ranges N to M is the same.
[XYZ] is considered consistent with any one of the text in brackets.
[^XYZ] is the opposite of the above.
[A-z] the range of text, from "a" to "Z", is considered consistent.
[^a-z] is the opposite of the above.
/b Indicates the end of the word. For example: "ER" of "er/b" and "never" are consistent, but inconsistent with "er" of "verb".
/b Indicates the end of the word.
/d represents a number.
/d indicates non-numeric.
/s represents a space.
/S denotes a non space.
/w represents all alphanumeric characters.
/w represents a non-all alphanumeric number.

I (ignore case)
G (all occurrences of pattern in full text search)
GI (Full-text lookup, ignore case)
/num num should be given a positive number. Compared with the already stored part. Example: "(.) /1 "and any two consecutive identical
Text is consistent.

Two how to define a text:
Method One: Write directly
var s=/regular expression/I or G or IG

Method Two: 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: Searches within the line of text that is drawn up.
Structure: Regular expression. EXEC (String).
Narrator: Retrieved return:
Null not retrieved;
A consistent result is retrieved;
Cases:
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 performance.
Structure: Regular expression. Compile (' body ', ' g ' or I or IG ').
Explanation: There is nothing to say.
Cases:
The code snippet is as follows:
<script>
var s= ' Aabbccddeeffgghhiijjkkllmmnnoopp '
var r=new RegExp (' [A-z] ', ' g ');
var a=s.match (R);
alert (a);
R.compile (' [A-z] ', ' g ');
var a=s.match (R);
alert (a);
</script>


3 test method:
Description: As the name suggests, do quiz.
Structure: Regular expression. Test (String).
Explanation: Return:
False not found;
True was found;
Cases:
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 same and replace him.
Structure: string. Replace (regular expression, replacement string).
Narrator: Does not change with the string and returns its copy.
Cases:
The code snippet is as follows:
<script>
var s= ' Aabbccddeeffgghhiijjkkllmmnnoopp '
var r=new RegExp (' [A-z] ', ' g ');
var a=s.replace (/[a-z]/g, ' a ');
alert (a);
</script>


5 Match Method:
Description: Implement the search.
Structure: string. Match (regular expression).
Explanation: Returns a series.
Cases:
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: Splits the string.
Structure: String. Split (regular expression).
Explanation: Returns a series.
Cases:
The code snippet is as follows:
<script>
var s= "Hello This is 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 useful than indexof!) )
Structure: String. Search (regular expression).
Explanation: Return
If a positive integer is found
-1 if not found
Cases:
The code snippet is as follows:
<script>
var s= "Hello This is 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 ("[A-Z]", "G");
S=s.replace (R, "a");
Alert (s)
</script>


And finally his various attributes

1 lastindex Properties:
Description: Sets the location where the retrieval starts and gets its value
Structure: Regular expression. lastindex (= value).
Explanation:
When the lastindex is greater than the length of the retrieved text, if executed with the Test,exec method, the execution fails,
The Lastindex property is set to 0.
When lastindex is equal to retrieving the length of the text, it is consistent if the expression body is empty. Other occasions,
Execution failed, reset to 0.
In addition to the above, the lastindex will be set to the position pointer of the last consistent text column.

2 Source Property
Description: Returns the body of a regular expression
Structure: Regular expression. source
Cases:
The code snippet is as follows:
<script>
var s=/[a-z]{3}/w/s/g;
var s1=new RegExp ("[a-z]{3}/w", "G");
alert (S.source);
alert (S1.source);
</script>

Here I write a few of the processing character functions:

1 The number is 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 Quasi-letters
The code snippet is as follows:
function Check (msg) {
var exe=//w/g;
if (Exe.test (msg)) return (0);
else return (1);
}


3 Prohibited Code
The code snippet is as follows:
function Check (msg) {
var exe=/< (/w|/w) *>/g;
if (Exe.test (msg)) return (0);
else return (1);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.