jsÖÐsplitµÄÓ÷¨
javascript ÖÐµÄ split() º¯Êý½«×Ö·û´®²ð·ÖΪÊý×飬ʹÓÃÍÑÀë·û£¨×Ö·û¡¢×Ö·û´®»òÕýÔò±í´ïʽ£©½«Æä·Ö³É¶à¸öÔªËØ£ºÌṩÍÑÀë·û²ÎÊý¼´¿É²ð·Ö×Ö·û´®¡£Ê¹ÓÃÕýÔò±í´ïʽ¿ÉʵÏÖ¸üÎÞаµÄ²ð·Ö¡£limit ²ÎÊýÏÞÖÆ·µ»ØÊý×éµÄÔªËØÊý¡£Ê¹ÓÃÕýÔò±í´ïʽ¿É×èÖ¹¿ÕÔªËØ¡£split() º¯Êý²»»áÐÞ¸ÄÔʼ×Ö·û´®£¬·µ»ØÒ»¸öÐÂÊý×é¡£
JavaScript ÖÐ split() º¯ÊýµÄÓ÷¨
JavaScript ÖÐµÄ split() º¯Êý½«×Ö·û´®²ð·ÖΪÊý×飬ʹÓÃÖ¸¶¨µÄ×Ö·û»òÕýÔò±í´ïʽ×÷ΪÍÑÀë·û¡£
Óï·¨
string.split(separator[, limit])
µÇ¼ºó¸´ÖÆ
²ÎÊý
separator£ºÓÃÓÚ²ð·Ö×Ö·û´®µÄÍÑÀë·û¡£¿ÉÒÔÊÇ×Ö·û¡¢×Ö·û´®»òÕýÔò±í´ïʽ¡£
limit£¨¿ÉÑ¡£©£ºÖ¸¶¨·µ»ØÊý×éÖеÄ×î´óÔªËØÊý¡£
·µ»ØÖµ
Ò»¸ö°üÀ¨²ð·Öºó×Ö·û´®ÔªËصÄÊý×é¡£
ʹÓÃ
Òª²ð·Ö×Ö·û´®£¬Ç뽫ÍÑÀë·û×÷Ϊ²ÎÊýת´ï¸ø split() º¯Êý¡£ÀýÈ磺
const str = "Hello,World,JavaScript"; const arr = str.split(","); console.log(arr); // Êä³ö£º["Hello", "World", "JavaScript"]
µÇ¼ºó¸´ÖÆ
¸ß¼¶Ó÷¨
ʹÓÃÕýÔò±í´ïʽ
split() º¯Êý»¹¿ÉÒÔʹÓÃÕýÔò±í´ïʽ×÷ΪÍÑÀë·û¡£ÕâÔÊÐí¾ÙÐиüÖØ´óºÍÎÞаµÄ²ð·Ö¡£
const str = "Hello123World456JavaScript"; const arr = str.split(/\d+/); console.log(arr); // Êä³ö£º["Hello", "World", "JavaScript"]
µÇ¼ºó¸´ÖÆ
Ö¸¶¨ÏÞÖÆ
limit ²ÎÊýÔÊÐíÏÞÖÆ·µ»ØÊý×éÖеÄÔªËØÊý¡£ÈôÊÇÊ¡ÂÔ£¬Ôòº¯Êý½«²ð·ÖÕû¸ö×Ö·û´®¡£
const str = "Hello,World,JavaScript"; const arr = str.split(",", 2); console.log(arr); // Êä³ö£º["Hello", "World"]
µÇ¼ºó¸´ÖÆ
×èÖ¹¿ÕÔªËØ
ÈôÊÇÍÑÀë·û·ºÆðÔÚ×Ö·û´®µÄ¿ªÍ·»ò×îºó£¬split() º¯Êý¿ÉÄܻᱬ·¢¿ÕÔªËØ¡£Ê¹ÓÃÕýÔò±í´ïʽ¿ÉÒÔÓÐÓÃ×èÖ¹ÕâÖÖÇéÐΡ£
const str = "Hello,,World,JavaScript"; const arr = str.split(/,\s*/); console.log(arr); // Êä³ö£º["Hello", "World", "JavaScript"]
µÇ¼ºó¸´ÖÆ
×¢ÖØ
split() º¯Êý¶ÔÔʼ×Ö·û´®²»»áÓÐÈκÎÐ޸ġ£Ëü·µ»ØÒ»¸öеÄÊý×é¡£
ÍÑÀë·ûÒ²¿ÉÒÔÊÇÈκΠUnicode ×Ö·û»ò×Ö·û´®¡£
ÈôÊDz»Ö¸¶¨ÍÑÀë·û£¬split() º¯Êý½«Æ¾Ö¤¿Õ¸ñ×Ö·û²ð·Ö×Ö·û´®¡£
ÒÔÉϾÍÊÇjsÖÐsplitµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡