index.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. // @ts-nocheck
  2. /**
  3. * 检测输入值是否为正则表达式对象
  4. * @param obj - 需要检测的任意类型值
  5. * @returns 如果检测值是正则表达式返回 true,否则返回 false
  6. *
  7. * @example
  8. * // 基础检测
  9. * isRegExp(/abc/); // true
  10. * isRegExp(new RegExp('abc')); // true
  11. *
  12. * @example
  13. * // 非正则表达式检测
  14. * isRegExp('hello'); // false
  15. * isRegExp({}); // false
  16. * isRegExp(null); // false
  17. *
  18. * @description
  19. * 1. 通过 Object.prototype.toString 的可靠类型检测
  20. * 2. 支持跨执行环境的可靠检测:
  21. * - 浏览器多 iframe 环境
  22. * - Node.js 的 vm 模块
  23. * 3. 比 instanceof 检测更可靠
  24. * 4. 支持 ES3+ 全环境兼容
  25. */
  26. export function isRegExp(obj : any) : boolean {
  27. // #ifndef APP-ANDROID
  28. return Object.prototype.toString.call(obj) === '[object RegExp]';
  29. // #endif
  30. // #ifdef APP-ANDROID
  31. return obj instanceof RegExp//Object.prototype.toString.call(obj) === '[object RegExp]';
  32. // #endif
  33. }