¾È±Ô °øºÎ¹æ

Javascript & JQuery & CSS > JQuery Á¤±Ô½Ä - Á¤±Ô½Ä ¸Þ¼Òµå - exec(), Replace(), match(), test()

µî·ÏÀÏ : 2017-07-14 17:19 Á¶È¸¼ö : 53,205

Á¤±Ô½Ä¿¡¼­ »ç¿ëÇÏ´Â ¸Þ¼­µåµé ÀÔ´Ï´Ù.

 
1. exec()

 ÀÏÄ¡ÇÏ´Â ¹®ÀÚ¿­À» ã´Â RegExp ¸Þ¼ÒµåÀÔ´Ï´Ù. Á¤º¸¸¦ °¡Áö°í ÀÖ´Â ¹è¿­À» ¹ÝȯÇÕ´Ï´Ù. console.log(myArray)¸¦ »ç¿ëÇÏ¸é ¹è¿­ÀÎ ["dbbd", "bb"]¸¦ ¹Ýȯ ÇÕ´Ï´Ù.
 

  
 var myRe = /d(b+)d/g;
 var myArray = myRe.exec("cdbbdbsbz");

 console.log(myArray)
 


2. replace()

 ÀÏÄ¡ÇÏ´Â ¹®ÀÚ¿­À» ã´Â String ¸Þ¼ÒµåÀÔ´Ï´Ù. ÀÏÄ¡ÇÏ´Â ¹®ÀÚ¿­À» replacement ·Î ´ëüÇÕ´Ï´Ù.
 ¿©±â¼­, $1, $2´Â ù¹ø°¿Í µÎ¹ø° ¼Ò°ýÈ£ÀÇ ¹®ÀÚ¿­À» ´ë½ÅÇϴ Ư¼ö±âÈ£¿Í °°½À´Ï´Ù.
 
 str ¹è¿­¿¡ ´ã°ÜÀÖ´Â John SmithÀÎ ¹®ÀÚ¿­¿¡¼­ re¿¡ Á¤ÀÇÇصРÁ¤±Ô½ÄÆÐÅÏÀ» »ç¿ëÇؼ­ ¾Õ µÚ¸¦ ¹Ù²ãÁÖ´Â °ÍÀÔ´Ï´Ù.
 
 console.log »ç¿ë½Ã °á°ú°ªÀº "Smith John" ÀÌ Ãâ·ÂµË´Ï´Ù.
 

  
 var re = /(\w+)\s(\w+)/;
 var str = "John Smith";
 var newstr = str.replace(re, "$2, $1");
 console.log(newstr); 
 


3. match()

 ÀÏÄ¡ÇÏ´Â ¹®ÀÚ¿­À» ã´Â String ¸Þ¼ÒµåÀÔ´Ï´Ù. Á¤º¸¸¦ °¡Áö°í ÀÖ´Â ¹è¿­À» ¹ÝȯÇϰųª ÀÏÄ¡ÇÏÁö ¾Ê´Â ºÎºÐÀ» null·Î ¹ÝȯÇÕ´Ï´Ù.
 i Ç÷¡±×¸¦ »ç¿ëÇؼ­ ´ë/¼Ò¹®ÀÚ ±¸ºÐ¾øÀÌ ¸ÅÄ¡ °¡´ÉÇÕ´Ï´Ù.
 

  
 var str = 'For more information, see Chapter 3.4.5.1';
 var re = /(chapter \d+(\.\d)*)/i;
 var found = str.match(re);
 console.log(found);
 


4. test()

 ÀÏÄ¡ÇÏ´Â ¹®ÀÚ¿­À» °Ë»çÇÏ´Â RegExp ¸Þ¼ÒµåÀÔ´Ï´Ù. true³ª false¸¦ ¹ÝȯÇÕ´Ï´Ù.
  

 function testinput(re, str){
   var midstring;
   if (re.test(str)) {
  midstring = ' contains ';
   } else {
  midstring = ' does not contain ';
   }
   console.log(str + midstring + re.source);
 }
 

 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.