/**
 * 校验字符串并返回结果及过滤后的字符串
 * @param {string} str - 待处理的字符串
 * @param {number[]} types - 需要检查/过滤的类型数组 (1-5)
 * @returns {object} 包含校验结果和过滤后的字符串
 */
function checkAndFilter(str, types) {
  // 校验规则映射表
  const checkRules = {
    1: { pattern: "\\d", regex: /\d/ },                       // 数字
    2: { pattern: "[a-zA-Z]", regex: /[a-zA-Z]/ },            // 字母
    3: { pattern: "\\u4e00-\\u9fa5", regex: /[\u4e00-\u9fa5]/ }, // 汉字
    4: { pattern: "[^\\w\\u4e00-\\u9fa5]", regex: /[^\w\u4e00-\u9fa5]/ }, // 特殊符号
    5: { pattern: "\\s", regex: /\s/ }                         // 空格
  };
  const patterns = types
    .filter(t => checkRules[t])
    .map(t => `(${checkRules[t].pattern})`)
    .join("|");
  const filterRegex = patterns ? new RegExp(patterns, "g") : null;
    const filteredStr = filterRegex ? str.replace(filterRegex, "") : str;
  const result = types.some(t => 
    checkRules[t] ? checkRules[t].regex.test(str) : false
  );
  return {
    result,
    filteredStr
  };
}使用示例
const str = "Hello123 你好_world!";
// 示例1:过滤数字(1)和空格(5)
const result1 = checkAndFilter(str, [1, 5]);
console.log(result1);
// 输出: { result: true, filteredStr: "Hello你好_world!" }
// 示例2:仅过滤汉字(3)
const result2 = checkAndFilter(str, [3]);
console.log(result2);
// 输出: { result: true, filteredStr: "Hello123 _world!" }
// 示例3:过滤不存在的类型(6)
const result3 = checkAndFilter("test", [6]);
console.log(result3);
// 输出: { result: false, filteredStr: "test" }主要改进说明
- 双模式处理: - regex字段用于快速检测字符存在性(- .test())
- pattern字段用于构建过滤正则表达式(- .replace())
 
- 智能过滤逻辑: - // 组合多个类型的正则表达式 // 例如 [1,5] 会生成 /(\d)|(\s)/g const patterns = types.map(t => `(${checkRules[t].pattern})`).join("|");
- 空类型处理: - // 当传入空数组或无效类型时,返回原字符串 checkAndFilter("abc", []) // => { result: false, filteredStr: "abc" }
- 全局过滤: - // 使用 g 标志替换所有匹配项 str.replace(filterRegex, "")
参数对应关系
| 类型 | 说明 | 正则表达式 | 示例过滤效果 | 
|---|---|---|---|
| 1 | 数字 | \d | "a1b" → "ab" | 
| 2 | 字母 | [a-zA-Z] | "中a文1" → "中1" | 
| 3 | 汉字 | [\u4e00-\u9fa5] | "hello你好" → "hello" | 
| 4 | 特殊符号 | [^\w\u4e00-\u9fa5] | "a!b#c" → "abc" | 
| 5 | 空格(含换行制表符) | \s | "a b\tc" → "abc" | 
该函数可以灵活处理各种字符过滤需求,例如:
- 清理输入内容:checkAndFilter(input, [4,5])移除所有特殊符号和空格
- 提取纯文本:checkAndFilter(text, [1,4,5]).filteredStr保留汉字和字母
- 密码强度校验:结合多个类型检测结果判断复杂度
[JS辅助方法] 校验字符串字符类型并返回结果及过滤后的字符串 by https://oneszhang.com/archives/136.html
如何在泛微中使用?
主表方法:
// 绑定「证件号」字段校验(禁止数字和特殊符号)
bindValidation("证件号", [1,4], "证件号禁止包含数字和特殊符号");
// 绑定「备注」字段校验(禁止空格和字母)
/** * 绑定字段校验规则 * @param {string} fieldName 表单字段名称(显示用名称) * @param {number[]} checkTypes 校验类型数组(1-5) * @param {string} message 校验不通过时的提示信息 */ function bindValidation(fieldName, checkTypes, message) { // 转换字段名为实际ID const fieldId = WfForm.convertFieldNameToId(fieldName); // 绑定字段变化事件 WfForm.bindFieldChangeEvent(fieldId, function(obj, id, value) { // 执行校验过滤 const { result, filteredStr } = checkAndFilter(value, checkTypes); if (result) { // 更新过滤后的值(自动触发二次校验) WfForm.changeFieldValue(id, { value: filteredStr }); // 显示提示信息 WfForm.showMessage(message); } }); }bindValidation("备注", [2,5], "备注内容不能包含字母和空格");
emmmmm,评论区没有格式化,将就看~~~@(勉强)