(I) What are regular expressions?
⑴ regular expressions, also known as regular expressions. (English: Regular Expression, the code is often abbreviated as regex, regexp or RE), a concept in computer science.
Regular expressions are typically used to retrieve, replace the text in line with those of a model (rule) is.
⑵ regular expression is a logical formula of string operations, is to use some combination of a particular pre-defined characters, and these particular character, form a "string rule."
This "rule string" is used to express a filtering logic of the string.
⑶ regular expressions, often abbreviated to "regex", the singular has regexp, regex, the complex has regexps, regexes, regexen.
(Ii) the purpose of regular expressions
Given a regular expression and another string, we can achieve the following purposes:
⑴ whether the given string regular expression filtering logic (referred to as "matching")
⑵ can get specific part of what we want from a string by a regular expression.
(Iii) the regular expression features
⑴ flexibility, logic and functionality is very strong
⑵ can quickly with a very simple way to achieve complex control string
⑶ For people who are new, relatively obscure
(Iv) regular expression syntax
/ Regular expression subject / modifier (optional)
Example:
in the Var patt = / to Baidu / i are
⑴ / baidu / i is a regular expression.
⑵ baidu body is a regular expression (for retrieval).
⑶ i is a modifier (search is case insensitive).
(V) use the string method
⑴ in JavaScript, the regular expression method commonly used for two strings: search () and replace ().
⑵search () method is used substring search character string specified, retrieving or regular expression matching substring, and return to the starting position of the substring.
⑶replace () method for replacing some characters other characters, or alternatively a substring match the positive expression in using string.
㈥search () method
⑴search () method uses a regular expression
Example:
Search for a regular expression " Runoob " string, case insensitive:
var STR = " Visit Runoob! " ;
Var n-str.search = (/ Runoob / I);
output is: 6
⑵search () method uses the string
string search method may be used as a parameter. String parameters are converted to regular expressions.
Example:
Retrieving the string " Runoob " substring:
var STR = " ! Visit Runoob " ;
var n-str.search = ( " Runoob " );
the output is: 6
㈦replace () method
⑴replace () method uses a regular expression
Example:
使用正则表达式且不区分大小写将字符串中的 Microsoft 替换为 Runoob :
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/microsoft/i,"Runoob");
结果输出为:Visit Runoob!
⑵replace() 方法使用字符串
replace() 方法将接收字符串作为参数:
示例:
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","Runoob");
★使用 RegExp 对象
在 JavaScript 中,RegExp 对象是一个预定义了属性和方法的正则表达式对象。
㈧使用 test()
⑴test() 方法是一个正则表达式方法。
⑵test() 方法用于检测一个字符串是否匹配某个模式,如果字符串中含有匹配的文本,则返回 true,否则返回 false。
⑶以下实例用于搜索字符串中的字符 "e":
⑷示例:
var patt = /e/;
patt.test("The best things in life are free!");
字符串中含有 "e",所以该实例输出为:true
㈨使用 exec()
⑴exec() 方法是一个正则表达式方法。
⑵exec() 方法用于检索字符串中的正则表达式的匹配。
⑶该函数返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。
⑷以下实例用于搜索字符串中的字母 "e":
/e/.exec("The best things in life are free!");
字符串中含有 "e",所以该实例输出为:e