A regular expression is a character sequence of search mode.
The search mode can be used to replace text search and text operations.
What are regular expressions?
A regular expression is a character sequence of search mode (search pattern) of.
When you search for text data, you can use the search mode to describe what you're searching.
Regular expressions can be a single character, or more complex patterns.
Regular expressions can be used to perform all types of text search and replace text operations.
grammar
/pattern/modifiers;
Examples
was patt = / Sysoft / in;
Example explained:
/ SYSOFT / i is a regular expression.
sysoft is a pattern (pattern) (used in a search).
i is a modifier (modifying the search is case insensitive).
Method string
In JavaScript, regular expressions are used in the two strings Method: Search () and replace ().
search () method using the expression to search for matching, and matches the return position.
replace () method returns the pattern to be replaced at the modified string.
The method of using the string search () to process the string
search () method takes a string as a search parameter. String parameter is converted to a regular expression:
Examples
Use string to perform a search for "sysoft" of:
var str = "Visit sysoft!"; var n = str.search("sysoft");
String in a regular expression method in search ()
Examples
Use regular expressions to perform a search string insensitive "sysoft" the case of search:
var str = "Visit sysoft"; var n = str.search(/sysoft/i);
n The result will be:
6
Use string method replace () String Processing
replace () accepts a string as a search parameter:
var str = "Visit Microsoft!"; var res = str.replace("Microsoft", "sysoft");
Using regular expressions in string method replace () in
Examples
Use case unknown to sysoft regular expressions to replace strings of Microsoft:
var str = "Visit Microsoft!"; var res = str.replace(/microsoft/i, "sysoft");
res The result will be:
Visit sysoft!
You noticed?
Regular expression parameter (rather than a string parameter) can be used in the above process.
Regular expressions can make your search more powerful (for example, not case-sensitive).
Regular expression modifiers
Modifiers can be used in a case-insensitive search more global elements:
Regular expression pattern
Find brackets for a range of string:
expression | description | |
---|---|---|
[abc] | Find any character between the brackets. | |
[0-9] | Find any digit from 0 to 9. | |
(X | y) | Find a | separated by any of the options. |
Metacharacter (Metacharacter) is to have a special meaning of characters:
Metacharacters | description | |
---|---|---|
\d | Find figures. | |
\s | Find a blank character. | |
\b | Match a word boundary. | |
\uxxxx | Find a hexadecimal number xxxx provisions of Unicode characters. |
Quantifiers definition of quantifiers:
quantifier | description | |
---|---|---|
n+ | Matches any string of at least one n. | |
n* | Matches any string contains zero or more occurrences of n. | |
n? | Matches any zero or a string containing n. |
Use RegExp object
In JavaScript, RegExp object is a regular expression object with predefined properties and methods.
Using the test ()
test () method is a regular expression.
It is to search through the string pattern, and return true or false based on the results.
The following example of a search string of characters "e":
Examples
var patt = /e/; patt.test("The best things in life are free!");
Because the string has a "e", the output of the code will be:
true
You do not have to first put a regular expression into variable. The above two lines as one line can be shortened:
/e/.test("The best things in life are free!");
Use exec ()
exec () method is a regular expression methods.
It (pattern) search string by specifying the mode and return the text has been found.
If no match is found, null is returned.
The following example of a search string of characters "e":
Examples
/e/.exec("The best things in life are free!");
Because the string has a "e", the output of the code will be:
E
------------------------------------------------- -------------------------------------------------- -------------------------------
source: www.sysoft.net.cn, plus v: 15844800162 depth exchange