Replace regular expression syntax in Javascript

Source: Internet
Author: User

Replace () is a simple replacement of characters.

ExampleCodeAs follows:

<Script language = "JavaScript">
VaR STRM = "javascript is a good script language ";
// Here I want to replace letter A with Letter
Alert (STRM. Replace ("A", ""));
</SCRIPT>

It only replaces the first letter. However, if you add a regular expression, the results will be different! Replace () supports regular expressions. It can match characters or strings according to the regular expression rules and then replace them!

Note: Do not add double quotation marks to the replaced part.

<Script language = "JavaScript">
VaR STRM = "javascript is a good script language ";
// Here I want to replace letter A with Letter
Alert (STRM. Replace (/A/, ""));
</SCRIPT>

In this case, only the first letter A is replaced.
<Script language = "JavaScript">
VaR STRM = "javascript is a good script language ";
// Replace all letters A with letters
Alert (STRM. Replace (/A/G, ""));
</SCRIPT>

As we can see from the above, when the regular expression has the "G" flag, it means that the entire string will be processed.

<Script language = "JavaScript">
VaR STRM = "javascript is a good script language ";
Alert (STRM. Replace (/(JavaScript) \ s * (is)/g, "$1 $2 fun. It $2 "));
</SCRIPT>

Let's take a look at a simple example: Replace the first letter of all words with uppercase letters.

<Script language = "JavaScript">
VaR STRM = "javascript is a good script language ";
Function Change (word)
{
Return word. indexof (0). touppercase () + word. substring (1 );
}
Alert (STRM. Replace (/\ B \ W + \ B/g, change ));
</SCRIPT>

As we can see from the above, when the regular expression has a "G" mark, it means that the entire string will be processed, that is, the transformation of the function change will be applied to all matching objects. This function has three or more parameters. The specific number depends on the regular expression.

With the combination of functions and regular expressions, replace () can process strings with unprecedented power!

In the end, it is easy to use Replace () to process all words in the string in reverse order.

<Script language = "JavaScript">
VaR STRM = "javascript is a good script language ";
Function Change (word)
{
VaR result = word. Match (/(\ W)/g );
If (result)
{
VaR STR = "";
For (VAR I = result. Length-1; I> = 0; I --)
{
STR + = result;
}
Return STR;
}
Else
{
Return "null ";
}
}
Alert (STRM. Replace (/\ B (\ W) + \ B/g, change ));
</SCRIPT>

Detailed explanation of Regular Expressions

Introduction

In short, regular expressions are a powerful tool for pattern matching and replacement. Its functions are as follows:
Test a mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called Data Validity verification.
Replace text. You can use a regular expression in a document to identify a specific text, and then delete it all or replace it with another text.
Extract a substring from the string based on the pattern match. It can be used to search for specific text in text or input fields.

Basic syntax

After a preliminary understanding of the functions and functions of a regular expression, let's take a look at the syntax format of the regular expression.

The regular expression format is generally as follows:

/Love/
The part between the "/" delimiters is the pattern to be matched in the target object. You only need to place the pattern content of the desired matching object between the "/" delimiters. To be usable
Users can customize the mode content more flexibly. Regular Expressions provide special "metacharacters ". Metacharacters are special characters that have special meanings in regular expressions and can be used to specify their leading characters (that is
The appearance mode of the object.
Frequently Used metacharacters include "+", "*", and "?".

The "+" metacharacter specifies that its leading character must appear one or more times consecutively in the target object.

The "*" metacharacter specifies that the leading character must appear zero or multiple times in the target object.

"?" Metacharacter specifies that the leading object must appear zero or once consecutively in the target object.

Next, let's take a look at the specific application of the regular expression metacharacters.

/FO +/because the regular expression above contains the "+" metacharacter, it can be used with the "fool", "FO ", or "football", and so on, one or more character strings that match the letter "F" consecutively.

/EG */because the above regular expression contains "*" metacharacters, it can be used with "easy", "ego ", or, "egg" and other strings that appear after the letter E are matched with zero or multiple Letter g consecutively.

/Wil? /Because the above regular expression contains "?" Metacharacter, indicating that it can match the "win" or "Wilson" in the target object, and matches zero or one character string after the letter I.

Sometimes I don't know how many characters to match. To adapt to this uncertainty, regular expressions support the concept of delimiters. These qualifiers can specify how many times a given component of a regular expression must appear to match.
{N} n is a non-negative integer. Match n times. For example, 'O {2} 'cannot match 'O' in "Bob", but can match two o in "food.

{N,} n is a non-negative integer. Match at least N times. For example, 'O {2,} 'cannot match 'O' in "Bob", but can match all o in "foooood. 'O {1,} 'is equivalent to 'o + '. 'O {0,} 'is equivalent to 'o *'.

Both {n, m} m and n are non-negative integers, where n <= m. Match at least N times and at most m times. For example, "O {1, 3}" matches the first three o in "fooooood. 'O {0, 1} 'is equivalent to 'o? '. Note that there must be no space between a comma and two numbers.

In addition to metacharacters, you can also precisely specify the frequency of occurrence of a pattern in a matching object. For example,

/Jim {}/the above regular expression specifies that the character m can appear 2-6 times consecutively in the matching object. Therefore, the above regular expression can match strings such as Jimmy or jimmmmmy.
After a preliminary understanding of how to use regular expressions, let's take a look at the usage of several other important metacharacters.
\ S: Used to match a single space character, including the tab key and line break;
\ S: Used to match all characters except a single space character;
\ D: Used to match numbers from 0 to 9;

\ W: Used to match letters, numbers, or underscores;
\ W: Used to match all characters that do not match \ W;
.: Used to match all characters except line breaks.
(Note: \ s and \ W can be regarded as inverse operations)
Next, let's take a look at how to use the above metacharacters in regular expressions through examples.
/\ S +/the above regular expression can be used to match one or more space characters in the target object.

/\ D000/if we have a complex financial statement in hand, we can easily find all the total amount of RMB through the above regular expression.
In addition to the metacharacters described above, regular expressions also have a unique special character, that is, the positioning character. Specifies the position where the matching mode appears in the target object. Commonly used positioning characters include "^", "$", "\ B", and "\ B ".
The "^" operator specifies that the matching mode must appear at the beginning of the target string. The "$" operator specifies that the matching mode must appear at the end of the target object.

The "\ B" Locator specifies that the matching mode must appear at the beginning or end of the target string.

The "\ B" Locator specifies that the matching object must be within the boundary of the start and end of the target string. That is, the matching object cannot start with the target string, it cannot end with the target string.
Similarly, we can regard "^" and "$" as well as "\ B" and "\ B" as two sets of operators for inverse operation. For example:

/^ Hell/because the regular expression above contains the "^" operator, you can use "hell" with the target object ",
The string starting with "hello" or "Hellhound" matches.
/AR $/because the regular expression above contains the "$" operator, you can
The string ending with "car", "bar", or "Ar" matches.
/\ Bbom/because the above regular expression mode starts with the "\ B" positioning character, it can be used with the target object
The string starting with "bomb" or "Bom" matches.
/Man \ B/because the above regular expression pattern ends with the "\ B" positioning character, it can be used with the target object
The string ending with "human", "Woman", or "man" matches.
To make it easier for users to set matching modes flexibly, regular expressions allow users to specify a range in the matching mode, not limited to specific characters. For example:
/[A-Z]/the above regular expression will match any uppercase letter from A to Z.

/[A-Z]/the above regular expression will match any lowercase letter in the range from A to Z.
/[0-9]/
The above regular expression will match any number from 0 to 9.

/([A-Z] [A-Z] [0-9]) +/the above regular expression will be associated with any string consisting of letters and numbers, for example, "ab0" matches.
Note that you can use "()" in a regular expression to combine strings. The content contained by the "()" symbol must appear in the target object at the same time. Therefore, the above regular expression cannot match strings such as "ABC", because the last character in "ABC" is a letter rather than a number.
If we want to implement the "or" operation similar to the programming logic in the regular expression, and select one of multiple different modes for matching, we can use the pipe character "| ". For example:
/To | too | 2/the above regular expression will match "to", "too", or "2" in the target object.
There is also a common operator in the regular expression, that is, the negative character "[^]". Different from the positioning character "^" described above
"[^]" Specifies that the target object cannot contain strings specified in the mode. For example:/[^ A-C]/the above string will match any character except A, B, and C in the target object. Average
When "^" appears in "[]", it is regarded as a negative operator. When "^" is outside of "[]" or "[]" is not displayed, it should be regarded as a positioning character.
Finally, you can use the Escape Character "\" to add metacharacters to the regular expression mode and find matching objects. For example, the/Th \ */regular expression will match the "th *" in the target object rather than ".
After constructing a regular expression, you can evaluate the value like a mathematical expression, that is, you can evaluate the value from left to right in a priority order. The priority is as follows:
1. \ Escape Character
2 .(),(? :),(? =), [] Parentheses and square brackets
3. *, + ,?, {N}, {n ,}, {n, m} qualifier
4. ^, $, \ anymetacharacter location and Sequence
5. | "or" Operation

example
JavaScript 1.2 contains a powerful Regexp () object, which can be used to match regular expressions. The test () method can check whether the target object contains the matching mode and return true or false accordingly.
we can use JavaScript to write the following script to verify the validity of the email address entered by the user.

onsubmit = "Return verifyaddress (this ); "> type =" text ">

Regular Expression object
This object contains the regular expression mode and a flag indicating how to apply the mode.
Syntax 1 Re =/pattern/[flags]
Syntax 2 Re = new Regexp ("pattern", ["Flags"]) [/Code]
Parameters
Re
Required. Name of the variable to be assigned a value in the regular expression mode.

Pattern
Required. The regular expression mode to use. If Syntax 1 is used, use the "/" character separation mode. If syntax 2 is used, quotation marks are used to mark the format.

Flags
Optional. If syntax 2 is used, the flag is enclosed in quotation marks. The flag can be used in combination and available include:
[Code] G (all pattern in full-text search) I (case-insensitive) m (multi-row search) [/Code]

Example
The following example creates an object (re) that contains the Regular Expression Pattern and related signs to demonstrate the usage of the regular expression object. In this example, the regular expression object used as the result is also used in the match method:
[Code] function matchdemo () {var R, RE; // declare the variable. VaR S = "The rain in
Spain falls mainly in the plain "; Re = new Regexp (" Ain "," G ");//
Create a regular expression object. R = S. Match (re); // search for matching in string S. Return (r);} [/Code]

Return Value: Ain, Ain \\
Attribute lastindex attribute | source attribute \\
Method compile method | exec method | Test Method \\
Required version 3 \\
See Regexp object | regular expression syntax | String object \\

Exec Method
Run the search in the string in regular expression mode and return an array containing the search result.
Rgexp.exe C (STR)

Parameters

Rgexp
Required. A regular expression object that contains the regular expression mode and available flag.

Str
Required. The string object or string text to be searched.

Description \\
If the exec method does not find a match, it returns NULL. If it finds a match, the exec method returns an array and updates the global Regexp.
To reflect the matching results. The 0 element of the array contains the complete match, and the 1st to nelement contains any child match in the match. This is equivalent to not setting the global flag)
.
If a global flag is set for the regular expression, exec searches for it at the position indicated by the value of lastindex. If the global flag is not set, exec ignores the value of lastindex and searches from the starting position of the string.

The array returned by the exec method has three attributes: input, index, and lastindex. Input
The attribute contains the entire searched string. The index attribute contains the position of the matched substring in the searched string. Lastindex
The attribute contains the next position of the last character in the match.

Example \\
The following example illustrates the usage of the exec method:
[Code] function regexptest () {var ver =
Number (scriptenginemajorversion () + "." + scriptengineminorversion ())
If (ver> = 5.5) {// test the JScript version. VaR src = "The rain in Spain
Falls mainly in the plain. "; var Re =/\ W +/g; // create the regular expression mode. VaR arr;
While (ARR = re.exe C (SRC ))! = NULL) document. Write (ARR. index + "-" +
Arr. lastindex + arr + "\ t") ;}else {alert ("Please use the updated version of JScript ");}}
[/Code]

Returned value: 0-3the 4-8rain 9-11in 12-17spain 18-23falls 24-30mainly 31-33in 34-37the 38-43plain

Test Method \\
Returns a Boolean value indicating whether the pattern exists in the searched string.
Rgexp. Test (STR)

Parameter \\
Rgexp
Required. A regular expression object that contains the regular expression mode or available flag.

Str
Required. The string to be searched.

Description
The test method checks whether a mode exists in the string. If yes, true is returned. Otherwise, false is returned.
The attributes of the global Regexp object cannot be modified by the test method.

Example
The following example illustrates the usage of the test method:
[Code] function testdemo (Re, S) {var S1; // declare the variable. // Check whether a regular expression exists in the string.
If (Re. test (s) // test whether the object exists. S1 = "contains"; // s inclusion mode. Else S1 ="
Does not contain "; // s does not contain the mode. Return ("'" + S + "'" + S1 + "'" +
Re. Source + "'"); // returns a string. } [/Code]

Function call: Document. Write (testdemo (/Ain +/, "The rain in Spain falls mainly in the plain ."));

Returned value: 'The rain in Spain falls mainly in the plain. 'containin' ain +'

Match Method
Use the regular expression mode to perform a query on the string and return the result containing the query as an array. \\
Stringobj. Match (rgexp)

Parameter \\
Stringobj
Required. String object or string text to search.

Rgexp
Required. It is a regular expression object that contains the regular expression mode and available flag. It can also be a variable name or string text that contains the regular expression mode and available signs.

Description \\
If no match is found in the match method, null is returned. If a match is found, an array is returned and the attributes of the global Regexp object are updated to reflect the matching result.
The array returned by the match method has three attributes: input, index, and lastindex. The INPUT attribute contains the entire searched string. Index
The attribute contains the position of the matched substring in the searched string. The lastindex attribute contains the next position of the last character in the last match.
If the global sign (G) is not set, the 0 element of the array contains the entire match, and the 1st to N element contains any child match that has occurred during the match. This is equivalent to the exec method without a global flag. If a global flag is set, elements 0 to N contain all matches.

Example \\
The following example demonstrates the usage of the match method:
[Code] function matchdemo () {var R, RE; // declare the variable. VaR S = "The rain in
Spain falls mainly in the plain "; Re =/Ain/I; // create the regular expression mode. R =
S. Match (re); // try to match the search string. Return (r); // return the place where "Ain" appears for the first time. } [/Code]
Return Value: Ain

This example describes how to use the match method with G flag settings.
[Code] function matchdemo () {var R, RE; // declare the variable. VaR S = "The rain in
Spain falls mainly in the plain "; Re =/Ain/ig; // create the regular expression mode. R =
S. Match (re); // try to match the search string. Return (r); // The returned array contains all the "Ain" // four matching results.
} [/Code]
Return Value: Ain, Ain

The above lines of code demonstrate the usage of the Match Method for string text.
[Code] var R, Re = "Spain"; r = "The rain in Spain". Replace (Re, "Canada"); return r; [/Code]
Return Value: The rain in Canada

Search Method
Returns the position of the first substring that matches the regular expression.

Stringobj. Search (rgexp)

Parameter \\
Stringobj
Required. String object or string text to be searched.

Rgexp
Required. A regular expression object that contains the regular expression mode and available flag.

Description

The search method specifies whether a matching exists. If a match is found, the search method returns an integer indicating the offset position starting from the string. If no match is found,-1 is returned.

Example \\
The following example demonstrates how to use the search method.
[Code] function searchdemo () {var R, RE; // declare the variable. VaR S = "The rain in
Spain falls mainly in the plain. "; Re =/falls/I; // create the regular expression mode. R =
S. Search (re); // search for strings. Return (r); // return the boolean result. } [/Code]
Return Value: 18

Regular expression syntax
A regular expression is a text mode consisting of common characters (such as characters A to Z) and special characters (such as metacharacters. This mode describes one or more strings to be matched when searching the text subject. A regular expression is used as a template to match a character pattern with the searched string.

Here are some examples of regular expressions that may be encountered:
[Code] JScript VBScript match/^ \ [\ t] * $/"^ \ [\ t] * $" matches a blank line.
/\ D {2}-\ D {5}/"\ D {2}-\ D {5}" verify that an ID number consists of two digits, A hyphen and a five-digit combination.
/<(. *)>. * <\/\ 1>/"<(. *)>. * <\/\ 1>" matches an HTML Tag.
[/Code]

The following table shows a complete list of metacharacters and their behaviors in the context of a regular expression:

Character Description
\ Mark the next character as a special character, a literal character, a back reference, or an octal escape character. For example, 'n' matches the character "N ". '\ N' matches a line break. The sequence '\' matches "\" and "\ (" matches "(".

^ Matches the start position of the input string. If the multiline attribute of the Regexp object is set, ^ matches the position after '\ n' or' \ R.

$ Matches the end position of the input string. If the multiline attribute of the Regexp object is set, $ also matches the position before '\ n' or' \ R.

* Matches the previous subexpression zero or multiple times. For example, Zo * can match "Z" and "Zoo ". * Is equivalent to {0 ,}.

+ Match the previous subexpression once or multiple times. For example, 'Zo + 'can match "zo" and "Zoo", but cannot match "Z ". + Is equivalent to {1 ,}.

? Match the previous subexpression zero or once. For example, "Do (ES )? "Can match" do "in" do "or" does ".? It is equivalent to {0, 1 }.

{N} n is a non-negative integer. Match n times. For example, 'O {2} 'cannot match 'O' in "Bob", but can match two o in "food.

{N,} n is a non-negative integer. Match at least N times. For example, 'O {2,} 'cannot match 'O' in "Bob", but can match all o in "foooood. 'O {1,} 'is equivalent to 'o + '. 'O {0,} 'is equivalent to 'o *'.

Both {n, m} m and n are non-negative integers, where n <= m. Match at least N times and at most m times. Liu, "O {1, 3}" will match the first three o in "fooooood. 'O {0, 1} 'is equivalent to 'o? '. Note that there must be no space between a comma and two numbers.

? When this character is followed by any other delimiter (*, + ,?, {N}, {n ,}, {n, m })
Later, the matching mode is non-greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example
"Oooo", 'O +? 'Will match a single "O", and 'O +' will match all 'O '.

. Match any single character except "\ n. To match any character including '\ n', use a pattern like' [. \ n.
(Pattern) matches pattern and obtains this match. The obtained match can be obtained from the generated matches set. It is used in VBScript.
Submatches set. In JScript, $0… is used... $9 attribute. To match the parentheses, use '\ (' or '\)'.

(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is using the "or" Character
(|) It is very useful to combine all parts of a mode. For example, 'industr (? : Y | ies'
A simpler expression.

(? = Pattern) Forward pre-query, in any match Pattern
To start from the string. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows
(? = 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 2000", but cannot match "Windows 3.1"
In
"Windows ". Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

(?! Pattern) negative pre-query, in any does not match negative lookahead matches the search string
At any point where a string not matching pattern
To start from the string. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows
(?! 95 | 98 | nt | 2000) 'can match "Windows" in "Windows 3.1", but cannot match "Windows 2000"
In Windows ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.

X | y matches X or Y. For example, 'z | food' can match "Z" or "food ". '(Z | f) Ood' matches "zood" or "food ".

[Xyz] Character Set combination. Match any character in it. For example, '[ABC]' can match 'A' in "plain '.

[^ XYZ] combination of negative character sets. Match any character not included. For example, '[^ ABC]' can match 'p' in "plain '.

[A-Z] character range. Matches any character in the specified range. For example, '[A-Z]' can match any lowercase letter in the range of 'A' to 'Z.

[^ A-Z] negative character range. Matches any character that is not within the specified range. For example, '[^ A-Z]' can match any character that is not in the range of 'A' to 'Z.

\ B matches a word boundary, that is, the position between a word and a space. For example, 'er \ B 'can match 'er' in "never", but cannot match 'er 'in "verb '.

\ B matches non-word boundaries. 'Er \ B 'can match 'er' in "verb", but cannot match 'er 'in "never '.

\ CX matches the control characters specified by X. For example, \ cm matches a control-M or carriage return character. The value of X must be either a A-Z or a-Z. Otherwise, C is treated as an original 'C' character.

\ D matches a numeric character. It is equivalent to [0-9].

\ D matches a non-numeric character. It is equivalent to [^ 0-9].

\ F matches a break. It is equivalent to \ x0c and \ Cl.

\ N matches a linefeed. It is equivalent to \ x0a and \ CJ.

\ R matches a carriage return. It is equivalent to \ x0d and \ cm.

\ S matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v].

\ S matches any non-blank characters. It is equivalent to [^ \ f \ n \ r \ t \ v].

\ T matches a tab. It is equivalent to \ x09 and \ CI.

\ V matches a vertical tab. It is equivalent to \ x0b and \ ck.

\ W matches any word characters that contain underscores. It is equivalent to '[A-Za-z0-9 _]'.

\ W matches any non-word characters. It is equivalent to '[^ A-Za-z0-9 _]'.

\ XN matches n, where N is the hexadecimal escape value. The hexadecimal escape value must be determined by the length of two numbers. For example, '\ x41' matches "". '\ X041' is equivalent to '\ x04' & "1 ". The regular expression can use ASCII encoding ..

\ Num matches num, where num is a positive integer. References to the obtained matching. For example, '(.) \ 1' matches two consecutive identical characters.

\ N identifies an octal escape value or a backward reference. If at least N subexpressions are obtained before \ n, n is a backward reference. Otherwise, if n is an octal digit (0-7), n is an octal escape value.

\ Nm identifies an octal escape value or a backward reference. If at least is preceded by at least nm exists before \ nm
And then nm is the backward reference. If at least N records are obtained before \ nm, n is a string followed by text M.
. If none of the preceding conditions are met, if n and m are Octal numbers (0-7), \ nm matches the octal escape value nm.

\ NML if n is an octal digit (0-3) and both M and l are octal digits (0-7), the octal escape value NML is matched.

\ UN matches n, where n is a Unicode character represented by four hexadecimal numbers. For example, \ u00a9 matches the copyright symbol (?).

Priority Order
After constructing a regular expression, you can evaluate the value like a mathematical expression, that is, you can evaluate the value from left to right in a priority order.

The following table lists the priority orders of various regular expression operators from the highest priority to the lowest priority:

Operator description \ escape character (),(? :),(? =), [] Parentheses and square brackets *, + ,?, {N}, {n ,}, {n, m} qualifier ^, $, \ anymetacharacter location and sequence | "or" Operation

Common characters

A common character consists of all the print and non-print characters that are not explicitly specified as metacharacters. This includes all uppercase and lowercase letter characters, all numbers, all punctuation marks, and some symbols.

The simplest regular expression is a single normal character that can match the character itself in the searched string. For example, the single-Character Mode 'A' can match the 'A' letter that appears at any position in the searched string '. Here are some examples of Single-character regular expression patterns:
[Code]/A // 7 // M/[/Code]
The equivalent single-character Regular Expression of VBScript is:
[Code] "A" "7" "M" [/Code]
You can combine multiple single characters to obtain a large expression. For example, the following JScript regular expression is an expression created by combining a single character expression 'A', '7', and 'M.

/A7m/
The equivalent VBScript expression is:

"A7m"
Note that there is no join operator here. All you need to do is place one character after the other.

Related Article

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.