//ÊÊÓñ¾ÎĵµES5ϵͳ°²×¿ JavaScriptÒýÇæRhino const ×Ö·û = { /** * Æ¥Åä²éÕÒ×Ö·û´®ÖеÄÄÚÈÝ * @param {string} str ÐèÒª²éÕÒµÄ×Ö·û´® * @param {string|RegExp} searchvalue ÐèÒª²éÕÒµÄ×Ö·û´®»òÕýÔò±í´ïʽ * @returns {Array|null} ³É¹¦:·µ»Ø°üº¬Æ¥Åä½á¹ûµÄÊý×é,ʧ°Ü:·µ»Ønull * @example * // ʾÀý1£º²éÕÒ×Ö·û´® * var str = "How are you doing you today?"; * var fgh = ×Ö·û.Æ¥Åä²éÕÒ(str, 'you'); * printl(fgh); // Êä³ö: ["you", "you"] * * // ʾÀý2£ºÊ¹ÓÃÕýÔò±í´ïʽ²éÕÒ * var str = "How are you doing you today?"; * var fgh = ×Ö·û.Æ¥Åä²éÕÒ(str, /you/gi); * printl(fgh); // Êä³ö: ["you", "you"] */ Æ¥Åä²éÕÒ: function(str, searchvalue) { try { // ²ÎÊýÑéÖ¤ if (typeof str === "undefined" || str === null) { console.error("[×Ö·û.Æ¥Åä²éÕÒ] ´íÎó£ºstr²ÎÊý䶨Òå»òΪnull"); return null; } if (typeof searchvalue === "undefined" || searchvalue === null) { console.error("[×Ö·û.Æ¥Åä²éÕÒ] ´íÎó£ºsearchvalue²ÎÊý䶨Òå»òΪnull"); return null; }
// ת»»Îª×Ö·û´® str = String(str); // Èç¹ûsearchvalueÊÇ×Ö·û´®£¬×ª»»ÎªÈ«¾ÖÕýÔò±í´ïʽ if (typeof searchvalue === 'string') { searchvalue = new RegExp(searchvalue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'); } // Èç¹ûÊÇÕýÔò±í´ïʽµ«Ã»ÓÐÈ«¾Ö±êÖ¾£¬Ìí¼ÓÈ«¾Ö±êÖ¾ else if (searchvalue instanceof RegExp && !searchvalue.global) { searchvalue = new RegExp(searchvalue.source, searchvalue.flags + 'g'); } // Ö´ÐÐÆ¥Åä²éÕÒ²Ù×÷ let matches = str.match(searchvalue); // Èç¹ûûÓÐÕÒµ½Æ¥ÅäÏ·µ»Ønull if (!matches || matches.length === 0) { return null; } return matches; } catch (error) { console.error(`[×Ö·û.Æ¥Åä²éÕÒ] ´íÎó£º${error}`); return null; } },
/** * Æ¥ÅäË÷Òý£¬²éÕÒ×Ö·û´®ÖÐÄÚÈݵÄλÖà * @param {string} str ÐèÒª²éÕÒµÄ×Ö·û´® * @param {string|RegExp} searchvalue ÐèÒª²éÕÒµÄ×Ö·û´®»òÕýÔò±í´ïʽ * @returns {Array|null} ³É¹¦:·µ»Ø°üº¬Æ¥ÅäλÖÃË÷ÒýµÄÊý×é,ʧ°Ü:·µ»Ønull * @example * // ʾÀý1£º²éÕÒ×Ö·û´®Î»Öà * var str = "How are you doing you today?"; * var fgh = ×Ö·û.Æ¥ÅäË÷Òý(str, 'you'); * printl(fgh); // Êä³ö: [8, 19] * * // ʾÀý2£ºÊ¹ÓÃÕýÔò±í´ïʽ²éÕÒλÖà * var str = "How are you doing you today?"; * var fgh = ×Ö·û.Æ¥ÅäË÷Òý(str, /you/gi); * printl(fgh); // Êä³ö: [8, 19] */ Æ¥ÅäË÷Òý: function(str, searchvalue) { try { // ²ÎÊýÑéÖ¤ if (typeof str === "undefined" || str === null) { console.error("[×Ö·û.Æ¥ÅäË÷Òý] ´íÎó£ºstr²ÎÊý䶨Òå»òΪnull"); return null; } if (typeof searchvalue === "undefined" || searchvalue === null) { console.error("[×Ö·û.Æ¥ÅäË÷Òý] ´íÎó£ºsearchvalue²ÎÊý䶨Òå»òΪnull"); return null; }
// ת»»Îª×Ö·û´® str = String(str); // ×¼±¸ÕýÔò±í´ïʽ let regex; if (typeof searchvalue === 'string') { // Èç¹ûÊÇ×Ö·û´®£¬×ª»»ÎªÕýÔò±í´ïʽ²¢×ªÒåÌØÊâ×Ö·û regex = new RegExp(searchvalue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'); } else if (searchvalue instanceof RegExp) { // Èç¹ûÒѾÊÇÕýÔò±í´ïʽ£¬È·±£ÓÐÈ«¾Ö±êÖ¾ if (!searchvalue.global) { regex = new RegExp(searchvalue.source, searchvalue.flags + 'g'); } else { regex = searchvalue; } } else { console.error("[×Ö·û.Æ¥ÅäË÷Òý] ´íÎó£ºsearchvalueÀàÐͲ»ÕýÈ·"); return null; } // ²éÕÒËùÓÐÆ¥ÅäµÄË÷Òý let indices = []; let match; // ÖØÖÃlastIndexÒÔÈ·±£´ÓÍ·¿ªÊ¼ËÑË÷ regex.lastIndex = 0; while ((match = regex.exec(str)) !== null) { indices.push(match.index); // ·ÀÖ¹Á㳤¶ÈÆ¥Åäµ¼ÖµÄÎÞÏÞÑ»· if (match.index === regex.lastIndex) { regex.lastIndex++; } } // Èç¹ûûÓÐÕÒµ½Æ¥ÅäÏ·µ»Ønull if (indices.length === 0) { return null; } return indices; } catch (error) { console.error(`[×Ö·û.Æ¥ÅäË÷Òý] ´íÎó£º${error}`); return null; } } };
// ʾÀý1£ºÊ¹ÓÃ×Ö·û´®²éÕÒ var str = "How are you doing you today?"; var fgh = ×Ö·û.Æ¥Åä²éÕÒ(str, 'you'); printl(fgh); // Êä³ö: ["you", "you"]
// ʾÀý2£ºÊ¹ÓÃÕýÔò±í´ïʽ²éÕÒ var str = "How are you doing you today?"; var fgh = ×Ö·û.Æ¥Åä²éÕÒ(str, /you/gi); printl(fgh); // Êä³ö: ["you", "you"]
// ʾÀý3£ºÊ¹ÓÃ×Ö·û´®²éÕÒË÷Òý var str = "How are you doing you today?"; var fgh = ×Ö·û.Æ¥ÅäË÷Òý(str, 'you'); printl(fgh); // Êä³ö: [8, 19]
// ʾÀý4£ºÊ¹ÓÃÕýÔò±í´ïʽ²éÕÒË÷Òý var str = "How are you doing you today?"; var fgh = ×Ö·û.Æ¥ÅäË÷Òý(str, /you/gi); printl(fgh); // Êä³ö: [8, 19]
1. ÕûÌå½á¹¹ÕâÊÇÒ»¸öÃûΪ×Ö·û µÄ¶ÔÏ󣬰üº¬Á½¸öÖ÷Òª·½·¨£º Æ¥Åä²éÕÒ : ÓÃÓÚ²éÕÒ×Ö·û´®ÖеÄÆ¥ÅäÄÚÈÝÆ¥ÅäË÷Òý : ÓÃÓÚ²éÕÒÆ¥ÅäÄÚÈÝÔÚ×Ö·û´®ÖеÄλÖÃ
2. Æ¥Åä²éÕÒ·½·¨ (×Ö·û.Æ¥Åä²éÕÒ)
×Ö·û.Æ¥Åä²éÕÒ(str, searchvalue)
²ÎÊý˵Ã÷£º str : ÒªËÑË÷µÄÄ¿±ê×Ö·û´®searchvalue : ËÑË÷Ìõ¼þ£¬¿ÉÒÔÊÇ×Ö·û´®»òÕýÔò±í´ïʽ
¹¦ÄÜÌص㣺 - ²ÎÊýÑéÖ¤£º
if (typeof str === "undefined" || str === null) {
console.error("[×Ö·û.Æ¥Åä²éÕÒ] ´íÎó£ºstr²ÎÊý䶨Òå»òΪnull");
return null;
}
- ¼ì²é²ÎÊýÊÇ·ñΪ¿Õ»ò䶨Òå
- ³ö´íʱ·µ»Ønull²¢´òÓ¡´íÎóÐÅÏ¢
- ×Ö·û´®´¦Àí£º
str = String(str);
- ½«ÊäÈëת»»Îª×Ö·û´®£¬È·±£¿ÉÒÔ½øÐÐÆ¥Åä²Ù×÷
- ËÑË÷Ìõ¼þ´¦Àí£º
if (typeof searchvalue === 'string') {
searchvalue = new RegExp(searchvalue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
}
- Èç¹ûÊÇ×Ö·û´®£¬×ª»»ÎªÕýÔò±í´ïʽ²¢×ªÒåÌØÊâ×Ö·û
- Èç¹ûÊÇÕýÔò±í´ïʽ£¬È·±£ÓÐÈ«¾Ö±êÖ¾
- Ö´ÐÐÆ¥Å䣺
let matches = str.match(searchvalue);
- ʹÓÃJavaScriptµÄmatch·½·¨½øÐÐÆ¥Åä
- ·µ»ØËùÓÐÆ¥ÅäÏîµÄÊý×é
3. Æ¥ÅäË÷Òý·½·¨ (×Ö·û.Æ¥ÅäË÷Òý)
×Ö·û.Æ¥ÅäË÷Òý(str, searchvalue)
²ÎÊý˵Ã÷£º - ÓëÆ¥Åä²éÕÒ·½·¨Ïàͬ£¬µ«·µ»ØÆ¥ÅäλÖöø²»ÊÇÆ¥ÅäÄÚÈÝ
¹¦ÄÜÌص㣺 - ²ÎÊýÑéÖ¤£ºÓëÆ¥Åä²éÕÒÏàͬ
- ÕýÔò±í´ïʽ´¦Àí£º
let regex;
if (typeof searchvalue === 'string') {
regex = new RegExp(searchvalue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
}
- ½«ËÑË÷Ìõ¼þͳһת»»ÎªÕýÔò±í´ïʽ
- ´¦ÀíÌØÊâ×Ö·ûºÍÈ«¾Ö±êÖ¾
- ²éÕÒλÖãº
while ((match = regex.exec(str)) !== null) {
indices.push(match.index);
}
- ʹÓÃexec·½·¨Ñ»·²éÕÒËùÓÐÆ¥Åä
- ¼Ç¼ÿ¸öÆ¥ÅäµÄλÖã¨Ë÷Òý£©
- ·ÀÖ¹ËÀÑ»·£º
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
- ´¦ÀíÁ㳤¶ÈÆ¥ÅäµÄÌØÊâÇé¿ö
- È·±£ËÑË÷Ö¸ÕëÇ°½ø
4. ´íÎó´¦ÀíÁ½¸ö·½·¨¶¼°üº¬ÍêÕûµÄ´íÎó´¦Àí»úÖÆ£º
try {
// ·½·¨Ö÷Ìå
} catch (error) {
console.error(`[×Ö·û.xxx] ´íÎó£º${error}`);
return null;
}
- ʹÓÃtry-catch²¶»ñËùÓпÉÄܵĴíÎó
- ³ö´íʱ·µ»Ønull²¢´òÓ¡Ïêϸ´íÎóÐÅÏ¢
5. ʹÓÃʾÀý´úÂë°üº¬ËĸöÍêÕûµÄʾÀý£º
// ʾÀý1£ºÊ¹ÓÃ×Ö·û´®²éÕÒÄÚÈÝ
var str = "How are you doing you today?";
var fgh = ×Ö·û.Æ¥Åä²éÕÒ(str, 'you');
printl(fgh); // Êä³ö: ["you", "you"]
// ʾÀý2£ºÊ¹ÓÃÕýÔò±í´ïʽ²éÕÒÄÚÈÝ
var fgh = ×Ö·û.Æ¥Åä²éÕÒ(str, /you/gi);
printl(fgh); // Êä³ö: ["you", "you"]
// ʾÀý3£ºÊ¹ÓÃ×Ö·û´®²éÕÒλÖÃ
var fgh = ×Ö·û.Æ¥ÅäË÷Òý(str, 'you');
printl(fgh); // Êä³ö: [8, 19]
// ʾÀý4£ºÊ¹ÓÃÕýÔò±í´ïʽ²éÕÒλÖÃ
var fgh = ×Ö·û.Æ¥ÅäË÷Òý(str, /you/gi);
printl(fgh); // Êä³ö: [8, 19]
6. ´úÂëÌصã- ½¡×³ÐÔ£º
- ÍêÕûµÄ²ÎÊýÑéÖ¤
- È«ÃæµÄ´íÎó´¦Àí
- ÌØÊâÇé¿öµÄ´¦Àí£¨ÈçÁ㳤¶ÈÆ¥Å䣩
- Áé»îÐÔ£º
- Ö§³Ö×Ö·û´®ºÍÕýÔò±í´ïʽÁ½ÖÖËÑË÷·½Ê½
- ×Ô¶¯´¦ÀíÕýÔò±í´ïʽ±êÖ¾
- תÒåÌØÊâ×Ö·û
- ¿Éά»¤ÐÔ£º
- ÇåÎúµÄ´úÂë½á¹¹
- ÏêϸµÄ×¢ÊͺÍÎĵµ
- ͳһµÄ´íÎó´¦Àí·½Ê½
- ʹÓñãÀû£º
- ¼òµ¥Ö±¹ÛµÄAPI
- ÍêÕûµÄʹÓÃʾÀý
- ÇåÎúµÄ·µ»ØÖµÔ¼¶¨
Õâ¶Î´úÂëÌṩÁËÒ»¸öÍêÕûµÄ×Ö·û´®²éÕÒ½â¾ö·½°¸£¬¼È¿ÉÒÔ»ñÈ¡Æ¥ÅäµÄÄÚÈÝ£¬Ò²¿ÉÒÔ»ñÈ¡Æ¥ÅäµÄλÖã¬ÊÊÓÃÓÚ¸÷ÖÖ×Ö·û´®´¦Àí³¡¾°¡£
|