JS Regular Description:
RegExp object represents a regular expression, it is a powerful tool for the implementation of the string pattern matching. A regular expression is a character sequence of search mode. The search mode can be used to replace text search and text operations.
Example:
var patt = /w3school/i;
// /w3school/是搜索模式;i(对搜索模式中的字母大小写不敏感)
//最后包含属性 "g"、"i" 和 "m",分别用于指定全局匹配、区分大小写的匹配和多行匹配
Direct amount syntax: / pattern / the Attributes
create RegExp object syntax: new new RegExp (pattern, the Attributes); // parameter pattern is a string that specifies a regular expression pattern or other regular expression. optional attributes is a string that contains the attribute "g", "i" and "m", are used to specify the global matching insensitive matching and multi-line matching.
Canonical correlation:
Square brackets:
Square brackets are used to find a range of characters:
expression | description |
---|---|
[abc] | Find any character between the brackets. |
[^abc] | Find any character not between the brackets. |
[0-9] | Find any digit from 0 to 9. |
[a-z] | Find any character from lowercase a to lowercase z's. |
[A-Z] | Find any character from uppercase A to Z of capital. |
[A-z] | Z Find any character from uppercase A to lowercase. |
[Adgk] | Finding given to any character in the set. |
[^ Adgk] | Find character given to anyone outside the set. |
(red|blue|green) | Find any options specified. |
Regular metacharacters:
Metacharacter (Metacharacter) is to have a special meaning of characters:
Metacharacters | description |
---|---|
. | Find a single character, except for line breaks and line endings. |
\w | Find a word character. |
\W | Find a non-word character. |
\d | Find figures. |
\D | Finding non-numeric characters. |
\s | Find a blank character. |
\S | Find a non-whitespace characters. |
\b | Match a word boundary. |
\B | Matching non-word boundary. |
\0 | Find NUL characters. |
\n | Find newline. |
\f | Find feed character. |
\r | Find carriage return. |
\t | Find tab. |
\ v | Find vertical tab. |
\xxx | Find octal number xxx defined characters. |
\ xdd | Find a hexadecimal number dd illegal characters. |
\ uxxxx | Find a hexadecimal number xxxx provisions of Unicode characters. |
Regular quantifiers:
quantifier | description |
---|---|
n+ | Matches any string containing at least one of n (one or more times). |
n* | Matches any string contains zero or more occurrences of n. |
n? | Matches any zero or a string containing n. |
n{X} | Matching string comprising the sequence of X n. |
n {X, Y} | It contains the string matching sequence X to Y number of n. |
n{X,} | X matching string comprising at least a sequence of n. |
n$ | Matches any string of n ends. |
^n | Matches any string that begins with n. |
?=n | Immediately followed by the specified string matches any character string n. |
?!n | Immediately thereafter not match any character string specified string n. |
RegExp object methods:
method | description | FF | IE |
---|---|---|---|
compile | Compiling a regular expression. | 1 | 4 |
exec | Value specified in the search character string. The return value of the find and determine its location. | 1 | 4 |
test | Value specified in the search character string. Returns true or false. | 1 | 4 |
It supports regular expressions String object methods
method | description | FF | IE |
---|---|---|---|
search | Retrieving the value of the regular expression to match. | 1 | 4 |
match | Find one or more regular expression matching. | 1 | 4 |
replace | Alternatively substring match the regular expression. | 1 | 4 |
split | String is divided into an array of strings. | 1 | 4 |
JS regular Example
Determine the user's input is not a valid phone number?
//test检验是否符合特定规范
//定义正则表达式
var phone = document.getElementById("txtphone").value;
//var msgstr = /^[0-9]{11}/;
//var result=msgstr.test(phone);//检验是否符合特定格式
//alert(result);
var msgstr = /\d{11}/g;//设置为全局变量,用于全部循环检索(实现原理:会记录查找到的索引位置和长度,跳过依次查询)
var result = msgstr.exec(msg);//默认只返回第一个结果,所以要循环输出
while (result) {//只要不是null或underfined就是找到结果了就是为True
// alert(result);//打印该值
result = msgstr.exec(msg);
};
Determine whether the mailbox user input is valid?
//邮箱格式检索
var emailstr = /\w+\.\w+@\w+\.(\w+)+/;
var res=emailstr.test(phone);
//alert(res);
Regular grouping queries:
//分组查询
var groupstr = /(\d{3})\d{4}(\d{4})/g;
var str = groupstr.exec(msg);
//结果第一组是匹配的字符串,第二组是第一个匹配,第三组是第二个匹配
//alert(str.toString());//13013582532 130 1358
String using regular expressions string substitution:
//字符串的替换
var results=msg.replace(/(\d{3})(\d{4})(\d{4})/g, '$1****$3');//$1指定字符串第一部分原样输出
//alert(results);
String uses regular expressions to remove the spaces on both sides:
//去除两端空格
var msgstr = " hellow ";
//将前后的空白换成空字符,空字符如果加空格就一个字符否则就是空字符也是去空格
var resstr = msgstr.replace(/^\s+/, '').replace(/\s+$/, '');
//
var s=msgstr.trim();//实现02
alert(s);