ÕýÔò±í´ïʽÖÐtest¡¢exec¡¢matchµÄÇø±ð½éÉܼ°À¨ºÅµÄÓ÷¨

תÔØ  2017-09-19   Ͷ¸å£ºmrr   ÎÒÒªÆÀÂÛ

ÕâƪÎÄÕÂÖ÷Òª½éÉÜÁËÕýÔò±í´ïʽÖÐtest¡¢exec¡¢matchµÄÇø±ð½éÉܼ°À¨ºÅµÄÓ÷¨,·Ç³£²»´í£¬¾ßÓвο¼½è¼ø¼ÛÖµ£¬ÐèÒªµÄÅóÓÑ¿ÉÒԲο¼ÏÂ

test¡¢exec¡¢matchµÄ¼òµ¥Çø±ð

1¡¢test

test ·µ»Ø Boolean£¬²éÕÒ¶ÔÓ¦µÄ×Ö·û´®ÖÐÊÇ·ñ´æÔÚģʽ¡£ var str = "1a1b1c"; var reg = new RegExp("1.", ""); alert(reg.test(str)); // true

2¡¢exec

exec ²éÕÒ²¢·µ»Øµ±Ç°µÄÆ¥Åä½á¹û£¬²¢ÒÔÊý×éµÄÐÎʽ·µ»Ø¡£ var str = "1a1b1c"; var reg = new RegExp("1.", ""); var arr = reg.exec(str); Èç¹û²»´æÔÚģʽ£¬Ôò arr Ϊ null£¬·ñÔò arr ×ÜÊÇÒ»¸ö³¤¶ÈΪ 1 µÄÊý×飬ÆäÖµ¾ÍÊǵ±Ç°Æ¥ÅäÏî¡£arr »¹ÓÐÈý¸öÊôÐÔ£ºindex µ±Ç°Æ¥ÅäÏîµÄλÖã»lastIndex µ±Ç°Æ¥ÅäÏî½áÊøµÄλÖã¨index + µ±Ç°Æ¥ÅäÏîµÄ³¤¶È£©£»input ÈçÉÏʾÀýÖÐ input ¾ÍÊÇ str¡£

exec ·½·¨ÊܲÎÊý g µÄÓ°Ïì¡£ÈôÖ¸¶¨ÁË g£¬ÔòÏ´ε÷Óà exec ʱ£¬»á´ÓÉϸöÆ¥ÅäµÄ lastIndex ¿ªÊ¼²éÕÒ¡£ var str = "1a1b1c"; var reg = new RegExp("1.", ""); alert(reg.exec(str)[0]); alert(reg.exec(str)[0]); ÉÏÊöÁ½¸öÊä³ö¶¼ÊÇ 1a¡£ÏÖÔÚÔÙ¿´¿´Ö¸¶¨²ÎÊý g£º var str = "1a1b1c"; var reg = new RegExp("1.", "g"); alert(reg.exec(str)[0]); alert(reg.exec(str)[0]); ÉÏÊöµÚÒ»¸öÊä³ö 1a£¬µÚ¶þ¸öÊä³ö 1b¡£

3¡¢match

match ÊÇ String ¶ÔÏóµÄÒ»¸ö·½·¨¡£ var str = "1a1b1c"; var reg = new RegExp("1.", ""); alert(str.match(reg)); match Õâ¸ö·½·¨ÓеãÏñ exec£¬µ«£ºexec ÊÇ RegExp ¶ÔÏóµÄ·½·¨£»math ÊÇ String ¶ÔÏóµÄ·½·¨¡£¶þÕß»¹ÓÐÒ»¸ö²»Í¬µã£¬¾ÍÊǶԲÎÊý g µÄ½âÊÍ¡£ Èç¹ûÖ¸¶¨Á˲ÎÊý g£¬ÄÇô match Ò»´Î·µ»ØËùÓеĽá¹û¡£ var str = "1a1b1c"; var reg = new RegExp("1.", "g"); alert(str.match(reg)); //alert(str.match(reg)); // ´Ë¾äͬÉϾäµÄ½á¹ûÊÇÒ»ÑùµÄ ´Ë½á¹ûΪһ¸öÊý×飬ÓÐÈý¸öÔªËØ£¬·Ö±ðÊÇ£º1a¡¢1b¡¢1c¡£

ÕýÔò±í´ïʽ¶ÔÏóÓÐÁ½¸ö¶¨Ò巽ʽ:£º

1¡¢µÚÒ»ÖÖ¶¨Ò壨¹¹Ô캯Êý¶¨Ò壩£º

new RegExp(pattern, attributes);Èçvar reg = new RegExp("abc","g")

ÆäÖÐpatternΪ±íʾ±í´ïʽÄÚÈÝ,ÈçÉϱíʾƥÅäabc

attributes£ºg,È«¾ÖÆ¥Å䣬i²»Çø·Ö´óСд£¬mÖ´ÐжàÐÐÆ¥Å䣬ÓÃ×î¶àµÄΪgºÍi

2¡¢µÚ¶þÖÖ¶¨Ò壨//Îı¾¶¨Ò壩:/pattern/attributes.

È磺var reg = /abc/g;

execºÍmatchµÄÏêϸÇø±ð£º

1¡¢execÊÇÕýÔò±í´ïʽµÄ·½·¨£¬¶ø²»ÊÇ×Ö·û´®µÄ·½·¨£¬ËüµÄ²ÎÊý²ÅÊÇ×Ö·û´®£¬ÈçÏÂËùʾ£º

ÈçÉ϶¨Òå 

var reg = new RegExp("abc") ; var str = "3abc4£¬5abc6"; reg.exec(str ); 

2¡¢matchÊÇ×Ö·û´®Ö´ÐÐÆ¥ÅäÕýÔò±í´ïʽ¹æÔòµÄ·½·¨£¬ËûµÄ²ÎÊýÊÇÕýÔò±í´ï£¬Èç

var reg = new RegExp("abc") ; var str = "3abc4,5abc6"; str.match(reg);

3¡¢execºÍmatch·µ»ØµÄ¶¼ÊÇÊý×飨ÕýÔò±í´ïʽÎÞ×Ó±í´ïʽ£¬²¢ÇÒ¶¨ÒåΪ·ÇÈ«¾ÖÆ¥Å䣩

Èç¹ûexecÖ´ÐеÄÕýÔò±í´ïʽûÓÐ×Ó±í´ïʽ(СÀ¨ºÅÄÚµÄÄÚÈÝ£¬Èç/abc(\s*)/ÖеÄ(\s*) )£¬Èç¹ûÓÐÆ¥Å䣬¾Í·µ»ØµÚÒ»¸öÆ¥ÅäµÄ×Ö·û´®ÄÚÈÝ£¬´ËʱµÄÊý×é½öÓÐÒ»¸öÔªËØ£¬Èç¹ûûÓÐÆ¥Åä·µ»Ønull;

var reg = new RegExp("abc") ; var str = "3abc4,5abc6"; alert(reg.exec(str)); alert(str.match(reg));

Ö´ÐÐÈçÉÏ´úÂ룬Äã»á·¢ÏÖÁ½ÕßÄÚÈݾùΪһÑù£ºabc£¬

4¡¢Èç¹û¶¨ÒåÕýÔò±í´ï¶ÔÏóΪȫ¾ÖÆ¥ÅäÈ磨ÕýÔò±í´ïʽÎÞ×Ó±í´ïʽ£¬²¢ÇÒ¶¨ÒåΪȫ¾ÖÆ¥Å䣩

var reg = new RegExp("abc","g") ; var str = "3abc4,5abc6"; alert(reg.exec(str)); alert(str.match(reg));

Ôò ΪabcºÍabc,abc£»ÒòΪmatchÖ´ÐÐÁËÈ«¾ÖÆ¥Åä²éѯ£»¶øexecÈç¹ûûÓÐ×Ó±í´ïʽֻ»áÕÒµ½Ò»¸öÆ¥ÅäµÄ¼´·µ»Ø¡£

5¡¢µ±±íʾÖк¬ÓÐ×Ó±í´ïʽµÄÇé¿ö£¨ÕýÔò±í´ïʽÓÐ×Ó±í´ïʽ£¬²¢ÇÒ¶¨ÒåΪ·ÇÈ«¾ÖÆ¥Å䣩£º

var reg = new RegExp("a(bc)") ; var str = "3abc4,5abc6"; alert(reg.exec(str)); alert(str.match(reg));

Äã»á·¢ÏÖÁ½ÕßÖ´ÐеĽá¹û¶¼ÊÇ£ºabc,bc;

6¡¢µ±Èç¹ûÕýÔò±í´ïʽ¶ÔÏó¶¨ÒåΪȫ¾ÖÆ¥Å䣨ÕýÔò±í´ïʽÓÐ×Ó±í´ïʽ£¬²¢ÇÒ¶¨ÒåΪȫ¾ÖÆ¥Å䣩

var reg = new RegExp("a(bc)","g") ; var str = "3abc4,5abc6"; alert(reg.exec(str)); alert(str.match(reg));

ÔòÁ½Õß·µ»ØµÄ½á¹ûÊÇabc,bcºÍabc,abc,

×ܽáΪ£º

1¡¢µ±ÕýÔò±í´ïʽÎÞ×Ó±í´ïʽ£¬²¢ÇÒ¶¨ÒåΪ·ÇÈ«¾ÖÆ¥Åäʱ£¬execºÍmatchÖ´ÐеĽá¹ûÊÇÒ»Ñù£¬¾ù·µ»ØµÚÒ»¸öÆ¥ÅäµÄ×Ö·û´®ÄÚÈÝ£»

2¡¢µ±ÕýÔò±í´ïʽÎÞ×Ó±í´ïʽ£¬²¢ÇÒ¶¨ÒåΪȫ¾ÖÆ¥Åäʱ£¬execºÍmatchÖ´ÐУ¬×ö´æÔڶദƥÅäÄÚÈÝ£¬Ôòmatch·µ»ØµÄÊǶà¸öÔªËØÊý×飻

3¡¢µ±ÕýÔò±í´ïʽÓÐ×Ó±íʾʱ£¬²¢ÇÒ¶¨ÒåΪ·ÇÈ«¾ÖÆ¥Å䣬execºÍmatchÖ´ÐеĽá¹ûÊÇÒ»ÑùÈçÉϱߵĵÚ5ÖÖÇé¿ö£»

4¡¢µ±ÕýÔò±í´ïʽÓÐ×Ó±íʾʱ£¬²¢ÇÒ¶¨ÒåΪȫ¾ÖÆ¥Å䣬execºÍmatchÖ´ÐеĽá¹û²»Ò»Ñù£¬´Ëʱmatch½«ºöÂÔ×Ó±í´ïʽ£¬Ö»²éÕÒÈ«Æ¥ÅäÕýÔò±í´ïʽ²¢·µ»ØËùÓÐÄÚÈÝ£¬ÈçÉϵÚ6ÖÖÇé¿ö£»

Ò²¾Í˵£¬execÓëÈ«¾ÖÊÇ·ñ¶¨ÒåÎÞ¹Øϵ£¬¶ømatchÔòÓÚÈ«¾ÖÏà¹ØÁª£¬µ±¶¨ÒåΪ·ÇÈ«¾Ö£¬Á½ÕßÖ´Ðнá¹ûÏàͬ

²»¹ÜÄÄÃÅÓïÑÔÖж¼ÓÐÀ¨ºÅ¡£ÕýÔò±í´ïʽҲÊÇÒ»ÃÅÓïÑÔ£¬¶øÀ¨ºÅµÄ´æÔÚʹÕâÃÅÓïÑÔ¸üΪǿ´ó¡£

¶ÔÀ¨ºÅµÄʹÓÃÊÇ·ñµÃÐÄÓ¦ÊÖ£¬ÊǺâÁ¿¶ÔÕýÔòµÄÕÆÎÕˮƽµÄÒ»¸ö²àÃæ±ê×¼¡£

À¨ºÅµÄ×÷Óã¬ÆäʵÈýÑÔÁ½Óï¾ÍÄÜ˵Ã÷°×£¬À¨ºÅÌṩÁË·Ö×飬±ãÓÚÎÒÃÇÒýÓÃËü¡£

ÒýÓÃij¸ö·Ö×飬»áÓÐÁ½ÖÖÇéÐΣºÔÚJavaScriptÀïÒýÓÃËü£¬ÔÚÕýÔò±í´ïʽÀïÒýÓÃËü¡£

±¾ÎÄÄÚÈÝËäÏà¶Ô¼òµ¥£¬µ«ÎÒҲҪд³¤µã¡£

ÄÚÈÝ°üÀ¨£º

1. ·Ö×éºÍ·ÖÖ§½á¹¹

2. ²¶»ñ·Ö×é

3. ·´ÏòÒýÓÃ

4. ·Ç²¶»ñ·Ö×é

5.Ïà¹Ø°¸Àý

1. ·Ö×éºÍ·ÖÖ§½á¹¹

Õâ¶þÕßÊÇÀ¨ºÅ×îÖ±¾õµÄ×÷Óã¬Ò²ÊÇ×îԭʼµÄ¹¦ÄÜ¡£

1.1 ·Ö×é

ÎÒÃÇÖªµÀ/a+/Æ¥ÅäÁ¬Ðø³öÏֵġ°a¡±£¬¶øҪƥÅäÁ¬Ðø³öÏֵġ°ab¡±Ê±£¬ÐèҪʹÓÃ/(ab)+/¡£

ÆäÖÐÀ¨ºÅÊÇÌṩ·Ö×鹦ÄÜ£¬Ê¹Á¿´Ê¡°+¡±×÷ÓÃÓÚ¡°ab¡±Õâ¸öÕûÌ壬²âÊÔÈçÏ£º

var regex = /(ab)+/g;
var string = "ababa abbb ababab";
console.log( string.match(regex) ); // ["abab", "ab", "ababab"]

1.2 ·ÖÖ§½á¹¹

¶øÔÚ¶àÑ¡·ÖÖ§½á¹¹(p1|p2)ÖУ¬´Ë´¦À¨ºÅµÄ×÷ÓÃÒ²ÊDz»ÑÔ¶øÓ÷µÄ£¬ÌṩÁË×Ó±í´ïʽµÄËùÓпÉÄÜ¡£

±ÈÈ磬ҪƥÅäÈçϵÄ×Ö·û´®£º

I love JavaScript
I love Regular Expression

¿ÉÒÔʹÓÃÕýÔò£º

var regex = /^I love (JavaScript|Regular Expression)$/;
console.log( regex.test("I love JavaScript") ); // true
console.log( regex.test("I love Regular Expression") ); // true

Èç¹ûÈ¥µôÕýÔòÖеÄÀ¨ºÅ£¬¼´/^I love JavaScript|Regular Expression$/£¬Æ¥Åä×Ö·û´®ÊÇ"I love JavaScript"ºÍ"Regular Expression"£¬µ±È»Õâ²»ÊÇÎÒÃÇÏëÒªµÄ¡£

2. ÒýÓ÷Ö×é

ÕâÊÇÀ¨ºÅÒ»¸öÖØÒªµÄ×÷Óã¬ÓÐÁËËü£¬ÎÒÃǾͿÉÒÔ½øÐÐÊý¾ÝÌáÈ¡£¬ÒÔ¼°¸üÇ¿´óµÄÌæ»»²Ù×÷¡£

¶øҪʹÓÃËü´øÀ´µÄºÃ´¦£¬±ØÐëÅäºÏʹÓÃʵÏÖ»·¾³µÄAPI¡£

ÒÔÈÕÆÚΪÀý¡£¼ÙÉè¸ñʽÊÇyyyy-mm-ddµÄ£¬ÎÒÃÇ¿ÉÒÔÏÈдһ¸ö¼òµ¥µÄÕýÔò£º

var regex = /\d{4}-\d{2}-\d{2}/;

È»ºóÔÙÐ޸ijÉÀ¨ºÅ°æµÄ£º

var regex = /(\d{4})-(\d{2})-(\d{2})/;

ΪʲôҪʹÓÃÕâ¸öÕýÔòÄØ£¿

2.1 ÌáÈ¡Êý¾Ý

±ÈÈçÌáÈ¡³öÄê¡¢Ô¡¢ÈÕ£¬¿ÉÒÔÕâô×ö£º

var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-06-12";
console.log( string.match(regex) ); 
// => ["2017-06-12", "2017", "06", "12", index: 0, input: "2017-06-12"]

match·µ»ØµÄÒ»¸öÊý×飬µÚÒ»¸öÔªËØÊÇÕûÌåÆ¥Åä½á¹û£¬È»ºóÊǸ÷¸ö·Ö×飨À¨ºÅÀƥÅäµÄÄÚÈÝ£¬È»ºóÊÇÆ¥Åäϱ꣬×îºóÊÇÊäÈëµÄÎı¾¡££¨×¢Ò⣺Èç¹ûÕýÔòÊÇ·ñÓÐÐÞÊηûg£¬match·µ»ØµÄÊý×é¸ñʽÊDz»Ò»ÑùµÄ£©¡£

ÁíÍâÒ²¿ÉÒÔʹÓÃÕýÔò¶ÔÏóµÄexec·½·¨£º

var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-06-12";
console.log( regex.exec(string) ); 
// => ["2017-06-12", "2017", "06", "12", index: 0, input: "2017-06-12"]

ͬʱ£¬Ò²¿ÉÒÔʹÓù¹Ô캯ÊýµÄÈ«¾ÖÊôÐÔ$1ÖÁ$9À´»ñÈ¡£º

var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-06-12";
regex.test(string); // ÕýÔò²Ù×÷¼´¿É£¬ÀýÈç
//regex.exec(string);
//string.match(regex);
console.log(RegExp.$1); // "2017"
console.log(RegExp.$2); // "06"
console.log(RegExp.$3); // "12"

2.2 Ìæ»»

±ÈÈ磬Ïë°Ñyyyy-mm-dd¸ñʽ£¬Ìæ»»³Émm/dd/yyyyÔõô×ö£¿

var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-06-12";
var result = string.replace(regex, "$2/$3/$1");
console.log(result); // "06/12/2017"

ÆäÖÐreplaceÖеģ¬µÚ¶þ¸ö²ÎÊýÀïÓÃ$1¡¢$2¡¢$3Ö¸´úÏàÓ¦µÄ·Ö×é¡£µÈ¼ÛÓÚÈçϵÄÐÎʽ£º

var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-06-12";
var result = string.replace(regex, function() {
 return RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1;
});
console.log(result); // "06/12/2017"

Ò²µÈ¼ÛÓÚ£º

var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-06-12";
var result = string.replace(regex, function(match, year, month, day) {
 return month + "/" + day + "/" + year;
});
console.log(result); // "06/12/2017"

3. ·´ÏòÒýÓÃ

³ýÁËʹÓÃÏàÓ¦APIÀ´ÒýÓ÷Ö×飬Ҳ¿ÉÒÔÔÚÕýÔò±¾ÉíÀïÒýÓ÷Ö×é¡£µ«Ö»ÄÜÒýÓÃ֮ǰ³öÏֵķÖ×飬¼´·´ÏòÒýÓá£

»¹ÊÇÒÔÈÕÆÚΪÀý¡£

±ÈÈçҪдһ¸öÕýÔòÖ§³ÖÆ¥ÅäÈçÏÂÈýÖÖ¸ñʽ£º

2016-06-12
2016/06/12
2016.06.12

×îÏÈ¿ÉÄÜÏëµ½µÄÕýÔòÊÇ:

var regex = /\d{4}(-|\/|\.)\d{2}(-|\/|\.)\d{2}/;
var string1 = "2017-06-12";
var string2 = "2017/06/12";
var string3 = "2017.06.12";
var string4 = "2016-06/12";
console.log( regex.test(string1) ); // true
console.log( regex.test(string2) ); // true
console.log( regex.test(string3) ); // true
console.log( regex.test(string4) ); // true

ÆäÖÐ/ºÍ.ÐèҪתÒå¡£ËäȻƥÅäÁËÒªÇóµÄÇé¿ö£¬µ«Ò²Æ¥Åä"2016-06/12"ÕâÑùµÄÊý¾Ý¡£

¼ÙÉèÎÒÃÇÏëÒªÇó·Ö¸î·ûÇ°ºóÒ»ÖÂÔõô°ì£¿´ËʱÐèҪʹÓ÷´ÏòÒýÓãº

var regex = /\d{4}(-|\/|\.)\d{2}\1\d{2}/;
var string1 = "2017-06-12";
var string2 = "2017/06/12";
var string3 = "2017.06.12";
var string4 = "2016-06/12";
console.log( regex.test(string1) ); // true
console.log( regex.test(string2) ); // true
console.log( regex.test(string3) ); // true
console.log( regex.test(string4) ); // false

×¢ÒâÀïÃæµÄ\1£¬±íʾµÄÒýÓÃ֮ǰµÄÄǸö·Ö×é(-|\/|\.)¡£²»¹ÜËüÆ¥Å䵽ʲô£¨±ÈÈç-£©£¬\1¶¼Æ¥ÅäÄǸöͬÑùµÄ¾ßÌåij¸ö×Ö·û¡£

ÎÒÃÇÖªµÀÁË\1µÄº¬Òåºó£¬ÄÇô\2ºÍ\3µÄ¸ÅÄîÒ²¾ÍÀí½âÁË£¬¼´·Ö±ðÖ¸´úµÚ¶þ¸öºÍµÚÈý¸ö·Ö×é¡£

¿´µ½ÕâÀ´Ëʱ£¬¿ÖÅÂÄã»áÓÐÈý¸öÎÊÌâ¡£

3.1 À¨ºÅǶÌ×Ôõô°ì£¿

ÒÔ×óÀ¨ºÅ£¨¿ªÀ¨ºÅ£©Îª×¼¡£±ÈÈ磺

var regex = /^((\d)(\d(\d)))\1\2\3\4$/;
var string = "1231231233";
console.log( regex.test(string) ); // true
console.log( RegExp.$1 ); // 123
console.log( RegExp.$2 ); // 1
console.log( RegExp.$3 ); // 23
console.log( RegExp.$4 ); // 3

ÎÒÃÇ¿ÉÒÔ¿´¿´Õâ¸öÕýÔòÆ¥Åäģʽ£º

µÚÒ»¸ö×Ö·ûÊÇÊý×Ö£¬±ÈÈç˵1£¬

µÚ¶þ¸ö×Ö·ûÊÇÊý×Ö£¬±ÈÈç˵2£¬

µÚÈý¸ö×Ö·ûÊÇÊý×Ö£¬±ÈÈç˵3£¬

½ÓÏÂÀ´µÄÊÇ\1£¬ÊǵÚÒ»¸ö·Ö×éÄÚÈÝ£¬ÄÇô¿´µÚÒ»¸ö¿ªÀ¨ºÅ¶ÔÓ¦µÄ·Ö×éÊÇʲô£¬ÊÇ123£¬

½ÓÏÂÀ´µÄÊÇ\2£¬ÕÒµ½µÚ2¸ö¿ªÀ¨ºÅ£¬¶ÔÓ¦µÄ·Ö×飬ƥÅäµÄÄÚÈÝÊÇ1£¬

½ÓÏÂÀ´µÄÊÇ\3£¬ÕÒµ½µÚ3¸ö¿ªÀ¨ºÅ£¬¶ÔÓ¦µÄ·Ö×飬ƥÅäµÄÄÚÈÝÊÇ23£¬

×îºóµÄÊÇ\4£¬ÕÒµ½µÚ3¸ö¿ªÀ¨ºÅ£¬¶ÔÓ¦µÄ·Ö×飬ƥÅäµÄÄÚÈÝÊÇ3¡£

Õâ¸öÎÊÌ⣬¹À¼Æ×Ðϸ¿´Ò»Ï£¬¾Í¸ÃÃ÷°×ÁË¡£

3.2 \10±íʾʲôÄØ£¿

ÁíÍâÒ»¸öÒÉÎÊ¿ÉÄÜÊÇ£¬¼´\10ÊDZíʾµÚ10¸ö·Ö×飬»¹ÊÇ\1ºÍ0ÄØ£¿´ð°¸ÊÇÇ°Õߣ¬ËäȻһ¸öÕýÔòÀï³öÏÖ\10±È½Ïº±¼û¡£²âÊÔÈçÏ£º

var regex = /(1)(2)(3)(4)(5)(6)(7)(8)(9)(#) \10+/;
var string = "123456789# ######"
console.log( regex.test(string) );

3.3 ÒýÓò»´æÔڵķÖ×é»áÔõÑù£¿

ÒòΪ·´ÏòÒýÓã¬ÊÇÒýÓÃÇ°ÃæµÄ·Ö×飬µ«ÎÒÃÇÔÚÕýÔòÀïÒýÓÃÁ˲»´æÔڵķÖ×éʱ£¬´ËʱÕýÔò²»»á±¨´í£¬Ö»ÊÇÆ¥Åä·´ÏòÒýÓõÄ×Ö·û±¾Éí¡£ÀýÈç\2£¬¾ÍÆ¥Åä"\2"¡£×¢Òâ"\2"±íʾ¶Ô2½øÐÐÁËתÒâ¡£

var regex = /\1\2\3\4\5\6\7\8\9/;
console.log( regex.test("\1\2\3\4\5\6\7\8\9") ); 
console.log( "\1\2\3\4\5\6\7\8\9".split("") );

chromeä¯ÀÀÆ÷´òÓ¡µÄ½á¹û£º

4. ·Ç²¶»ñ·Ö×é

֮ǰÎÄÖгöÏֵķÖ×飬¶¼»á²¶»ñËüÃÇÆ¥Åäµ½µÄÊý¾Ý£¬ÒÔ±ãºóÐøÒýÓã¬Òò´ËÒ²³ÆËûÃÇÊDz¶»ñÐÍ·Ö×é¡£

Èç¹ûÖ»ÏëÒªÀ¨ºÅ×îԭʼµÄ¹¦ÄÜ£¬µ«²»»áÒýÓÃËü£¬¼´£¬¼È²»ÔÚAPIÀïÒýÓã¬Ò²²»ÔÚÕýÔòÀï·´ÏòÒýÓᣴËʱ¿ÉÒÔʹÓ÷Dz¶»ñ·Ö×é(?:p)£¬ÀýÈç±¾ÎĵÚÒ»¸öÀý×Ó¿ÉÒÔÐÞ¸ÄΪ£º

var regex = /(?:ab)+/g;
var string = "ababa abbb ababab";
console.log( string.match(regex) ); // ["abab", "ab", "ababab"]

5. Ïà¹Ø°¸Àý

ÖÁ´ËÀ¨ºÅµÄ×÷ÓÃÒѾ­½²ÍêÁË£¬×ܽáÒ»¾ä»°£¬¾ÍÊÇÌṩÁ˿ɹ©ÎÒÃÇʹÓõķÖ×飬ÈçºÎÓþͿ´ÎÒÃǵÄÁË¡£

5.1 ×Ö·û´®trim·½·¨Ä£Äâ

trim·½·¨ÊÇÈ¥µô×Ö·û´®µÄ¿ªÍ·ºÍ½áβµÄ¿Õ°×·û¡£ÓÐÁ½ÖÖ˼·ȥ×ö¡£

µÚÒ»ÖÖ£¬Æ¥Åäµ½¿ªÍ·ºÍ½áβµÄ¿Õ°×·û£¬È»ºóÌæ»»³É¿Õ×Ö·û¡£È磺

function trim(str) {
 return str.replace(/^\s+|\s+$/g, '');
}
console.log( trim(" foobar ") ); // "foobar"

µÚ¶þÖÖ£¬Æ¥ÅäÕû¸ö×Ö·û´®£¬È»ºóÓÃÒýÓÃÀ´ÌáÈ¡³öÏàÓ¦µÄÊý¾Ý£º

function trim(str) {
 return str.replace(/^\s*(.*?)\s*$/g, "$1");
}
console.log( trim(" foobar ") ); // "foobar"

ÕâÀïʹÓÃÁ˶èÐÔÆ¥Åä*?£¬²»È»Ò²»áÆ¥Åä×îºóÒ»¸ö¿Õ¸ñ֮ǰµÄËùÓпոñµÄ¡£

µ±È»£¬Ç°ÕßЧÂʸߡ£

5.2 ½«Ã¿¸öµ¥´ÊµÄÊ××Öĸת»»Îª´óд

function titleize(str) {
 return str.toLowerCase().replace(/(?:^|\s)\w/g, function(c) {
 return c.toUpperCase();
 });
}
console.log( titleize('my name is epeli') ); // "My Name Is Epeli"

˼·ÊÇÕÒµ½Ã¿¸öµ¥´ÊµÄÊ××Öĸ£¬µ±È»ÕâÀﲻʹÓ÷Dz¶»ñÆ¥ÅäÒ²ÊÇ¿ÉÒԵġ£

5.3 Íշ廯

function camelize(str) {
 return str.replace(/[-_\s]+(.)?/g, function(match, c) {
 return c ? c.toUpperCase() : '';
 });
}
console.log( camelize('-moz-transform') ); // MozTransform

Ê××Öĸ²»»áת»¯Îª´óдµÄ¡£ÆäÖзÖ×é(.)±íʾÊ××Öĸ£¬µ¥´ÊµÄ½ç¶¨£¬Ç°ÃæµÄ×Ö·û¿ÉÒÔÊǶà¸öÁ¬×Ö·û¡¢Ï»®ÏßÒÔ¼°¿Õ°×·û¡£ÕýÔòºóÃæµÄ?µÄÄ¿µÄ£¬ÊÇΪÁËÓ¦¶Ôstrβ²¿µÄ×Ö·û¿ÉÄܲ»Êǵ¥´Ê×Ö·û£¬±ÈÈçstrÊÇ'-moz-transform '¡£

5.4 Öл®Ïß»¯

function dasherize(str) {
 return str.replace(/([A-Z])/g, '-$1').replace(/[-_\s]+/g, '-').toLowerCase();
}
console.log( dasherize('MozTransform') ); // -moz-transform

Íշ廯µÄÄæ¹ý³Ì¡£

5.5 htmlתÒåºÍ·´×ªÒå

// ½«HTMLÌØÊâ×Ö·ûת»»³ÉµÈÖµµÄʵÌå
function escapeHTML(str) {
 var escapeChars = {
 '¢' : 'cent',
 '£' : 'pound',
 '¥' : 'yen',
 '€': 'euro',
 '©' :'copy',
 '®' : 'reg',
 '<' : 'lt',
 '>' : 'gt',
 '"' : 'quot',
 '&' : 'amp',
 '\'' : '#39'
 };
 return str.replace(new RegExp('[' + Object.keys(escapeChars).join('') +']', 'g'), function(match) {
 return '&' + escapeChars[match] + ';';
 });
}
console.log( escapeHTML('<div>Blah blah blah</div>') );
// => <div>Blah blah blah</div>

ÆäÖÐʹÓÃÁËÓù¹Ô캯ÊýÉú³ÉµÄÕýÔò£¬È»ºóÌæ»»ÏàÓ¦µÄ¸ñʽ¾ÍÐÐÁË£¬Õâ¸ö¸ú±¾ÎÄû¶à´ó¹Øϵ¡£

µ¹ÊÇËüµÄÄæ¹ý³Ì£¬Ê¹ÓÃÁËÀ¨ºÅ£¬ÒÔ±ãÌṩÒýÓã¬Ò²ºÜ¼òµ¥£¬ÈçÏ£º

// ʵÌå×Ö·ûת»»ÎªµÈÖµµÄHTML¡£
function unescapeHTML(str) {
 var htmlEntities = {
 nbsp: ' ',
 cent: '¢',
 pound: '£',
 yen: '¥',
 euro: '€',
 copy: '©',
 reg: '®',
 lt: '<',
 gt: '>',
 quot: '"',
 amp: '&',
 apos: '\''
 };
 return str.replace(/\&([^;]+);/g, function(match, key) {
 if (key in htmlEntities) {
 return htmlEntities[key];
 }
 return match;
 });
}
console.log( unescapeHTML('<div>Blah blah blah</div>') );
// => <div>Blah blah blah</div>

ͨ¹ýkey»ñÈ¡ÏàÓ¦µÄ·Ö×éÒýÓã¬È»ºó×÷Ϊ¶ÔÏóµÄ¼ü¡£

5.6 Æ¥Åä³É¶Ô±êÇ©

ÒªÇóÆ¥Å䣺

<title>regular expression</title>
<p>laoyao bye bye</p>

²»Æ¥Å䣺

<title>wrong!</p>

Æ¥ÅäÒ»¸ö¿ª±êÇ©£¬¿ÉÒÔʹÓÃÕýÔò<[^>]+>£¬

Æ¥ÅäÒ»¸ö±Õ±êÇ©£¬¿ÉÒÔʹÓÃ<\/[^>]+>£¬

µ«ÊÇÒªÇóÆ¥Åä³É¶Ô±êÇ©£¬ÄǾÍÐèҪʹÓ÷´ÏòÒýÓã¬È磺

var regex = /<([^>]+)>[\d\D]*<\/\1>/;
var string1 = "<title>regular expression</title>";
var string2 = "<p>laoyao bye bye</p>";
var string3 = "<title>wrong!</p>";
console.log( regex.test(string1) ); // true
console.log( regex.test(string2) ); // true
console.log( regex.test(string3) ); // false

ÆäÖпª±êÇ©<[^>]+>¸Ä³É<([^>]+)>£¬Ê¹ÓÃÀ¨ºÅµÄÄ¿µÄÊÇΪÁ˺óÃæʹÓ÷´ÏòÒýÓ㬶øÌṩ·Ö×é¡£±Õ±êǩʹÓÃÁË·´ÏòÒýÓã¬<\/\1>¡£

ÁíÍâ[\d\D]µÄÒâ˼ÊÇ£¬Õâ¸ö×Ö·ûÊÇÊý×Ö»òÕß²»ÊÇÊý×Ö£¬Òò´Ë£¬Ò²¾ÍÊÇÆ¥ÅäÈÎÒâ×Ö·ûµÄÒâ˼¡£

×ܽá

ÒÔÉÏËùÊöÊÇС±à¸ø´ó¼Ò½éÉܵÄÕýÔò±í´ïʽÖÐtest¡¢exec¡¢matchµÄÇø±ð½éÉܼ°À¨ºÅµÄÓ÷¨£¬Ï£Íû¶Ô´ó¼ÒÓÐËù°ïÖú£¬Èç¹û´ó¼ÒÓÐÈκÎÒÉÎÊÇë¸øÎÒÁôÑÔ£¬Ð¡±à»á¼°Ê±»Ø¸´´ó¼ÒµÄ¡£ÔÚ´ËÒ²·Ç³£¸Ðл´ó¼Ò¶Ô½Å±¾Ö®¼ÒÍøÕ¾µÄÖ§³Ö£¡

×îÐÂÆÀÂÛ