Using rcipt -- Regular

Source: Internet
Author: User
Tags spl
Use Regular Expressions

VaR regstring = new Regexp ("string ");

By default, the regular expression matches the first string that appears. If you want global match, you need to add the parameter "G"

VaR regstring = new Regexp ("string", "G ");

At the same time, the regular expression is case-sensitive by default. If you want to make the regular expression case-insensitive, the parameter "I" is required"

VaR regstring = new Regexp ("string", "Gi ");

You can also use the following Perl-style Regular Expressions:

VaR regstring =/string/GI;

 

Regular Expression functions and Parameters

 

VaR Sm = "a bat, a cat, a fat bat, a fat cat ";
VaR par =//Gi;
Alert (SM.Search (PAR)); Output: 3

 

The exex () method has a string parameter and returns an array. The first entry in the array is the first match, and the other is reverse reference.
VaR s0000par.exe C (Sm);
Alert (s); output:

The match () method of the string object returns all matching arrays containing the string. This method calls the string object and passes it to the Regexp object.
VaR matchs = Sm.Match (PAR );Alert (matchs); output: at,

 

 

Extended string method


VaR par2 =//G;
VaR sp = Sm.Replace (par2, "% ");
Alert (SP); output: a B %, a C %, a fat bat, a fat C %

Replace (old, new) replaces the first parameter with the second parameter

VaR SPL = Sm.Split (",");
Alert (SPL );

// Flag (parameter)
// I case-insensitive matching
// G Global match. Find all matches instead of stopping the first match.
// M multi-row match. At this time, ^ matches the start of the string and the beginning of each line, $ matches the end of each line and the end of the string

Metacharacters

([{\ ^ $ | )*? +.

To use metacharacters, you must use \ for escape.

// Method of String object supporting regular expressions

The search () method is used to retrieve the specified substring in a string or a substring that matches a regular expression. If a match is found, the start position of the string is returned. Otherwise,-1 is returned.

The match () method can be used to search for a specified value in a string or to find a match between one or more regular expressions. Returns an array of matching results. The content of this array depends on whether Regexp has a global flag.

The Replace () method is used to replace other characters with some characters in a string, or replace a substring that matches a regular expression. This method returns a new string.

The split () method is used to split a string into a string array. This method returns a new string. The operations executed by string. Split () are the opposite to those executed by array. Join.

 

/N line feed
/R press ENTER
/T Tab
/V vertical Tab
/F page feed
[……]
[^…]
/S any blank space character
/W any acⅱ single character, equivalent to [a-zA-Z0-9 _]
/D any single number, equivalent to [0-9]

/S any non-blank characters
/W any non-acⅱ single character, equivalent to [^ a-zA-Z0-9 _]
/D any non-single number, equivalent to [^ 0-9]

^ Match the string Header
$ Match the end of a string
Any character except the line break and Terminator
? 0 or 1 time
+ 1 or multiple times
* 0 or multiple times
{N} n times
{N, m} Should be at least N times, but not more than m times
{N,} n to infinity

Non-Greedy repetition [add after repeated characters?]
??
+?
*?
{N, m }?

Select |
Group ()
Only combine (? :......)
Reference \ n must be used together with the group, in the group order (from left to right)

 

Regexp Object Attributes

Attribute
Global g
Ignorecase I
Multiline m
Source/[AB]/I => [AB]
Lastindex

Method

Exec () and match () are in the same global mode. A matching array is returned. Otherwise, null is returned.
Test () returns true and false, which is equivalent to calling exec ().

The compile () method is used to compile regular expressions during script execution. It can also be used to change and recompile regular expressions.

 

###############

{

VaR retest =/[Ba]/I;
Alert (retest. Global );
Alert (retest. ignorcase );
Alert (retest. multiline );
Alert (retest. source );

}

{

VaR stomatch = "BBQ is short for barbecue ";
VaR par =/B/g;
Par.exe C (stomatch );
Alert (par. lastindex );
Par.exe C (stomatch );
Alert (par. lastindex );
Par.exe C (stomatch );
Alert (par. lastindex );

 
}
 
{Compile ()
 
VaR STR = "every man in the world! Every woman on earth! ";
 
Patt =/man/g; str2 = Str. replace (patt, "person"); document. write (str2 + "<br/>"); patt =/(WO )? Man/g; Re-compiled pattPatt. Compile (patt );Str2 = Str. Replace (patt, "person"); document. Write (str2 );
 
}

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.