JavaScript compile() ·½·¨

¶¨ÒåºÍÓ÷¨

compile() ·½·¨ÓÃÓÚÔڽű¾Ö´Ðйý³ÌÖбàÒëÕýÔò±í´ïʽ¡£

compile() ·½·¨Ò²¿ÉÓÃÓڸıäºÍÖØÐ±àÒëÕýÔò±í´ïʽ¡£

Óï·¨

RegExpObject.compile(regexp,modifier)
²ÎÊý ÃèÊö
regexp ÕýÔò±í´ïʽ¡£
modifier ¹æ¶¨Æ¥ÅäµÄÀàÐÍ¡£"g" ÓÃÓÚÈ«¾ÖÆ¥Å䣬"i" ÓÃÓÚÇø·Ö´óСд£¬"gi" ÓÃÓÚÈ«¾ÖÇø·Ö´óСдµÄÆ¥Åä¡£

ʵÀý

ÔÚ×Ö·û´®ÖÐÈ«¾ÖËÑË÷ "man"£¬²¢Óà "person" Ìæ»»¡£È»ºóͨ¹ý compile() ·½·¨£¬¸Ä±äÕýÔò±í´ïʽ£¬Óà "person" Ìæ»» "man" »ò "woman"£¬£º

<script type="text/javascript">

var str="Every man in the world! Every woman on earth!";

patt=/man/g;
str2=str.replace(patt,"person");
document.write(str2+"<br />");

patt=/(wo)?man/g;
patt.compile(patt);
str2=str.replace(patt,"person");
document.write(str2);

</script>

Êä³ö£º

Every person in the world! Every woperson on earth!
Every person in the world! Every person on earth!

TIY

TIY

compile()
ÈçºÎʹÓà compile() À´¸Ä±äÕýÔò±í´ïʽ¡£
VUE