| |  |  |  |  |  | //适用本文档ES5系统安卓 JavaScript引擎Rhinoconst 字符 = {
 /**
 * 匹配查找字符串中的内容
 * @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. 代码特点健壮性:
 完整的参数验证全面的错误处理特殊情况的处理(如零长度匹配)
 灵活性:
 支持字符串和正则表达式两种搜索方式自动处理正则表达式标志转义特殊字符
 可维护性:
 使用便利:
 这段代码提供了一个完整的字符串查找解决方案,既可以获取匹配的内容,也可以获取匹配的位置,适用于各种字符串处理场景。 
                             
 |  |  |  |  |  | 
 |