1.1 RegExpʵĴ
ʽģʽ
gʾȫģʽģʽַǷֵһƥʱֹͣ
iʾִСдԴСд
mʾģʽ
Ԫַת壬Ԫַ( ) [ ] { } \ ^ $ ? * .
һʽԣ
var expression1 = /pattern /flags ;
var expression2 = new RegExp("pattern","flags");
var exp1 = /[bc]at/i;
var exp2 = new RegExp("[bc]at","i"); // exp1ǵȼ۵
1.2 RegExpʵ
ÿRegExpʵԣ
globalֵʾǷ g ־
ignoreCaseֵʾǷ i ־
lastIndexʾʼһƥַλã0
multilineֵʾǷ m ־
sourceʽַʾʽǴ빹캯ַģʽأ磺[bc]at
1.3 ʽȫŽ
ַ
\
һַΪһַһԭַһ áһ˽ת磬'n' ƥַ "n"'\n' ƥһз '\\' ƥ "\" "\(" ƥ "("
^
ƥַĿʼλá RegExp Multiline ԣ^ Ҳƥ '\n' '\r' ֮λá
$
ƥַĽλáRegExp Multiline ԣ$ Ҳƥ '\n' '\r' ֮ǰλá
*
ƥǰӱʽλΡ磬zo* ƥ "z" Լ "zoo"* ȼ{0,}
+
ƥǰӱʽһλΡ磬'zo+' ƥ "zo" Լ "zoo"ƥ "z"+ ȼ {1,}
?
ƥǰӱʽλһΡ磬"do(es)?" ƥ "do" "does" е"do" ? ȼ {0,1}
{n}
n һǸƥȷ n Ρ磬'o{2}' ƥ "Bob" е 'o'ƥ "food" е o
{n,}
n һǸƥn Ρ磬'o{2,}' ƥ "Bob" е 'o'ƥ "foooood" е o'o{1,}' ȼ 'o+''o{0,}' ȼ 'o*'
{n,m}
m n ΪǸn <= mƥ n ƥ m Ρ磬"o{1,3}" ƥ "fooooood" еǰ o'o{0,1}' ȼ 'o?'עڶź֮䲻пո
?
ַκһƷ (*, +, ?, {n}, {n,}, {n,m}) ʱƥģʽǷ̰ġ̰ģʽٵƥַĬϵ̰ģʽܶƥַ磬ַ "oooo"'o+?' ƥ䵥 "o" 'o+' ƥ 'o'
.
ƥ "\n" ֮κεַҪƥ '\n' ڵκַʹ '[.\n]' ģʽ
(pattern)
ƥ pattern ȡһƥ䡣ȡƥԴӲ Matches ϵõVBScript ʹ SubMatches ϣJScript ʹ $0$9 ԡҪƥԲַʹ '\(' '\)'
(?:pattern)
ƥ pattern ȡƥҲ˵һǻȡƥ䣬д洢Ժʹáʹ "" ַ (|) һģʽĸǺá磬 'industr(?:y|ies) һ 'industry|industries' Եıʽ
(?=pattern)
Ԥ飬κƥ pattern ַʼƥַһǻȡƥ䣬Ҳ˵ƥ䲻ҪȡԺʹá磬'Windows (?=95|98|NT|2000)' ƥ "Windows 2000" е "Windows" ƥ "Windows 3.1" е "Windows"Ԥ鲻ַҲ˵һƥ䷢һƥ֮ʼһƥǴӰԤַ֮ʼ
(?!pattern)
Ԥ飬κβƥ pattern ַʼƥַһǻȡƥ䣬Ҳ˵ƥ䲻ҪȡԺʹá'Windows (?!95|98|NT|2000)' ƥ "Windows 3.1" е "Windows"ƥ "Windows 2000" е "Windows"Ԥ鲻ַҲ˵һƥ䷢һƥ֮ʼһƥǴӰԤַ֮ʼ
x|y
ƥ x y磬'z|food' ƥ "z" "food"'(z|f)ood' ƥ "zood" "food"
[xyz]
ַϡƥһַ磬 '[abc]' ƥ "plain" е 'a'
[^xyz]
ֵַϡƥδַ磬 '[^abc]' ƥ "plain" е'p'
[a-z]
ַΧƥָΧڵַ磬'[a-z]' ƥ 'a' 'z' ΧڵСдĸַ
[^a-z]
ֵַΧƥκβָΧڵַ磬'[^a-z]' ƥκβ 'a' 'z' Χڵַ
\b
ƥһʱ߽磬ҲָʺͿոλá磬 'er\b' ƥ"never" е 'er'ƥ "verb" е 'er'
\B
ƥǵʱ߽硣'er\B' ƥ "verb" е 'er'ƥ "never" е 'er'
\cx
ƥ x ָĿַ磬 \cM ƥһ Control-M سx ֵΪ A-Z a-z ֮һ c Ϊһԭ 'c' ַ
\d
ƥһַȼ [0-9]
\D
ƥһַȼ [^0-9]
\f
ƥһҳȼ \x0c \cL
\n
ƥһзȼ \x0a \cJ
\r
ƥһسȼ \x0d \cM
\s
ƥκοհַոƱҳȵȡȼ [ \f\n\r\t\v]
\S
ƥκηǿհַȼ [^ \f\n\r\t\v]
\t
ƥһƱȼ \x09 \cI
\v
ƥһֱƱȼ \x0b \cK
\w
ƥ»ߵκεַȼ'[A-Za-z0-9_]'
\W
ƥκηǵַȼ '[^A-Za-z0-9_]'
\xn
ƥ n n ΪʮתֵʮתֵΪȷֳ磬'\x41' ƥ "A"'\x041' ȼ '\x04' & "1"ʽпʹ ASCII 롣.
\num
ƥ num num һȡƥá磬'(.)\1' ƥַͬ
\n
ʶһ˽תֵһá \n ֮ǰ n ȡӱʽ n Ϊá n Ϊ˽ (0-7) n Ϊһ˽תֵ
\nm
ʶһ˽תֵһá \nm ֮ǰ nm ӱʽ nm Ϊá \nm ֮ǰ n ȡ n Ϊһ m áǰ㣬 n m Ϊ˽ (0-7) \nm ƥ˽תֵ nm
\nml
n Ϊ˽ (0-3) m l Ϊ˽ (0-7)ƥ˽תֵ nml
\un
ƥ n n һĸʮֱʾ Unicode ַ磬 \u00A9 ƥȨ (?)
1.4 RegExpʵ
1exec()
÷רΪƵġexec() һҪӦģʽַȻذһƥϢ飻ûƥ·nullصȻArrayʵԣindex input index ʾƥַеλã input ʾӦʽַеһģʽƥַģʽвƥַģʽûв飬ֻһ
ƴ
var text = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;
var matches = pattern.exec(text);
console.log(matches);// 0: "mom and dad and baby", 1: " and dad and baby", 2: " and baby", groups: undefined, index: 0, input: "mom and dad and baby", length: 3
console.log(pattern.lastIndex); // 20
matches = pattern.exec(text);
console.log(matches); //null
console.log(pattern.lastIndex); // 0
text = "vat ,bat, sat, fat";
var pattern1 = /.at/;
var matches = pattern1.exec(text);
console.log(matches.index); //0
console.log(matches); //0: "vat", groups: undefined, index: 0, input: "vat ,vat, sat, fat", length: 1
console.log(pattern1.lastIndex); //0
matches = pattern1.exec(text);
console.log(matches.index); //0
console.log(matches); //0: "vat", groups: undefined, index: 0, input: "vat ,vat, sat, fat", length: 1
console.log(pattern1.lastIndex); //0
var pattern2 = /.at/g;
matches = pattern2.exec(text);
console.log(matches.index); //0
console.log(matches); //0: "vat", groups: undefined, index: 0, input: "vat ,vat, sat, fat", length: 1
console.log(pattern1.lastIndex); //0
matches = pattern2.exec(text);
console.log(matches.index); //5
console.log(matches); //0: "bat", groups: undefined, index: 5, input: "vat ,vat, sat, fat", length: 1
console.log(pattern1.lastIndex); //0
matches = pattern2.exec(text);
console.log(matches.index); //10
console.log(matches); //0: "sat", groups: undefined, index: 10, input: "vat ,vat, sat, fat", length: 1
console.log(pattern1.lastIndex); //0
ƴ
exec() ʹģʽȫֱʾgÿηҲֻһƥûȫֱ־ͬһַö exec() ʼշصһƥϢȫֱ־ÿε exec() ַлƥ
2test()
÷һַģʽIJƥ· true falseֻ֪Ŀַijģʽƥ䣬Ҫ֪ݵ
var text = "000-00-000";
var pattern = /\d{3}-\d{2}-\d{3}/;
if(pattern.test(text)){ //true
console.log("the pattern was matched!"); // ִ
}
1.5 RegExpľ̬
̬RegExpöĹԡЩ̬ԣҪʵֱӵá
øʽRegExp.attribute
Ե˵ԣ
var desc = 'Hello,everyone.My name is gtshen';
reg = /na(.?)/g;
reg.test(desc);
δΪ˵
- input
ܣصǰҪƥַ
ʾ console.log('input:'+RegExp.input) // -> 'Hello,everyone.My name is gtshen'
RegExp.$_;
ע⣺opera Ͱ汾֧֡
- lastMatch
ܣһƥ䵽ƥҪη-g
ʾ console.log('lastMatch:'+RegExp.lastMatch) // -> nam
RegExp['$&'];
ע⣺opera Ͱ汾֧֡
- lastParen
ܣһƥIJ顣Ҫη-g
ʾ console.log('lastParen:'+RegExp.lastParen) // -> 'm';
RegExp['$+'];
ע⣺opera Ͱ汾֧֡
- leftContext
ܣԵǰƥ䵽ӴΪģ֮ǰӴ
ʾ console.log('leftContext:'+RegExp.leftContext) // -> 'Hello,everyone.My ';
RegExp['$&`'];
- rightContext
ܣԵǰƥ䵽ӴΪģ֮Ӵ
ʾ console.log('rightContext:'+RegExp.rightContext) // -> 'e is gtshen';
RegExp['$\''];
- multiline
ܣǷֶ֧СֵΪbooleanֵtrueʾ֧֣falseʾ֧֡
ʾ console.log('multiline:'+RegExp.multiline);
RegExp['$*'];
ע⣺IE ֧֡
- $1 - $9
ܣ1 - 9ֵ
ʾ console.log('$1:'+ RegExp.$1) // -> 'm'
* עǡRegExpָһڳнƥʵ
2String е
1match()
match() һʽΪʽȫ g ÷ RegExp exec() ִнһȫֱ־ g ʱһƥĴ顣
ʽstr.match(pattern)
ܣ
match ڹԴķexecơ
match ƥpatternƥַָstrƥɹһʽĽڴƥıйصϢûƥ䵽null
ƴ
var text = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/i;
var matches = text.match(pattern);
console.log(matches);// 0: "mom and dad and baby", 1: " and dad and baby", 2: " and baby", groups: undefined, index: 0, input: "mom and dad and baby", length: 3
console.log(pattern.lastIndex); // 20
var pattern = /mom( and dad( and baby)?)?/gi;
matches = text.match(pattern);
console.log(matches);// ["mom and dad and baby"]
console.log(pattern.lastIndex); // 0
text = "vat ,bat, sat, fat";
pattern = /.at/;
// pattern.exec(text) ͬ
matches = text.match(pattern);
console.log(matches); //0: "vat", groups: undefined, index: 0, input: "vat ,vat, sat, fat", length: 1
console.log(pattern.lastIndex); //0
//matchƥȫgԣômatchصƥһƥĴ
pattern = /.at/g;
matches = text.match(pattern);
console.log(matches); //["vat", "bat", "sat", "fat"]
console.log(pattern.lastIndex); //0
ƴ
2search()
÷һʽַΪַͷҡ
ʽstr.search(pattern)
ܣƥpatternַмָĽظýĸԭַе-1书indexOfֻindexOf֧ƥ䡣
÷ȫηgҲ֧lastIndexҲζܱεãһֹͣ
var text = "vat ,bat, sat, fat";
var pattern = /at/;
var pos = text.search(pattern);
console.log(pos); // 1
console.log( text.search("t") ); // 2
3replace()
ʽstr.replace(pattern,given)
ܣƥpatternָgivenȥ滻str䲿ַpatternstrеIJַҲһʽ
pattern ַҲʽgiven滻ָݣȿַҲһص
replace() ֻ᷵ԭַĸԭַиĶ
pattern ַôֻ滻һƥַҪ滻ƥӴҪ pattern ʽҴȫֱ־ g
ƴ
var text = "cat ,bat, sat, fat";
var pattern = "at";
var result = text.replace(pattern,"ond");
console.log(result); // "cond ,bat, sat, fat"
pattern = /at/g;
result = text.replace(pattern,"ond");
console.log(result); //"cond ,bond, sond, fond"
var str = 'ңԶľ ľ Ҿվǰ ȴ֪Ұ㡣',
st = ['','',''],
pattern = '',
alt = '',
str1= "";
for(var i=0;i
pattern = new RegExp(st[i],'g');
for(var j=0;j
}
str1 = str.replace(pattern,alt);
alt = '';
}
console.log(str); //ңԶľ ľ Ҿվǰ ȴ֪Ұ㡣
console.log(str1); //ңԶ** *** Ҿվǰ ȴ֪*㡣
ƴ
givenǻص
ֻһƥûв£
ʽΪ function(match,index,input){}
ж飬
ʽΪ function(match,[$1...$9],index,input){}
match ʾǰƥĽ
[$1 - $9] ڷ£ʾǰ 1 - 9 ݡ
index ǰƥĸַ
input ƥԭַ
ƴ
function htmlEscape(text){
return text.replace(/[<>"&]/g,function(match,index,input){
switch(match){
case "<": return "<";
case ">": return ">";
case "&": return "&";
case "\"": return """;
}
});
}
console.log(htmlEscape("
Hello world!
"));//Hello world!
ngα̬˵
$arg_[parameter] httpijֵ/index.html?size=100,$arg_sizeȡ100$binary_remote_addr ƸʽĿͻ˵ַ$body_bytes_sent ʾͻ˷͵httpӦУ岿ֵֽ$content_length ʾͻͷеContent-Lengthֶ$contenttype ʾͻͷеContent-Typeֶ$cookie[cookie] ʾڿͻͷеcookieֶ$document_root ʾǰʹõrootֵ$uri ʾǰURIκβ$document_uri ͬ$request_uri ʾͻ˷ԭʼURIIJ$uri$document_uriδûԭʼڲضضURI$requesturiԶı䣬ʼǿͻԭʼURI$host ʾͻͷеHostֶΡHostֶβڣʵʴserverƴ档$hostname ʾNginxڻơ$http[header] ʾǰhttpӦͷֵ$senthttp[header] ʾؿͻ˵httpӦӦͷֵ$is_args ʾеURIǷ$is_argeֵΪΪַ$limit_rate ʾǰӵǶ٣0ʾ$nginx_version Nginxİ汾$query_string URIеIJ$argsͬ$query_stringֻIJı$remote_addr ʾͻ˵ַ$remote_port ʾͻ˶˿$remote_user ʾʹAuth Basic Moduleʱû$request_filename ʾûURIrootaliasתļ·$request_body ʾhttpеİ壬òֻproxy_passfastcgi_pass$request_body_file ʾhttpеİ洢ʱļ$request_completion ѾȫʱֵΪ"ok"ûɣҪؿͻˣֵΪַڶϵʹHTTP rangeʵIJļһ飬ôֵҲǿַ$request_method ʾhttpķGETPUTPOST$scheme ʾhttp schemenginx.com/бʾhttps$server_addr ʾַ$server_name ʾ$server_port ʾ˿$server_protocol ʾͻ˷ӦЭ飬HTTP/1.1HTTP/1.0ߣJesonʦӣhttps://www.imooc.com/article/22217ԴĽ
js еʽRegExp
https://www.cnblogs.com/zhanglw456/p/10442477.html