找回密码
 立即注册
首页 业界区 安全 JavaScript 替换字符串中的变量

JavaScript 替换字符串中的变量

轮达 2025-5-31 23:32:38
[code]const isNull = (obj) => obj === null;const isUndefined = (obj) => typeof obj === 'undefined';const isNullOrUndefined = (obj) => isUndefined(obj) || isNull(obj);const isObject = (obj) => !isNullOrUndefined(obj) && typeof obj === 'object' && !Array.isArray(obj)function stringify(data) {  switch (typeof data) {    case 'undefined':      return 'undefined'    case 'boolean':      return data ? 'true' : 'false'    case 'number':      return String(data)    case 'string':      return data    case 'symbol':      return data.toString()    case 'function':      return data.toString()    case 'object':      if (isNull(data)) {        return 'null'      }      if (data instanceof Error) {        return data.toString()      }      if (data instanceof Date) {        return data.toISOString()      }      return JSON.stringify(data, null, 2)    default:      return 'unknown'  }}function escapeRegex(str) {  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');}function hasFunction(obj, method) {  return (    isObject(obj) &&    method in obj &&    typeof obj[method] === 'function'  );};Date.prototype.format = function(fmt) {  const padZero = (num) => num.toString().padStart(2, '0');  const map = {    'YYYY': this.getFullYear(),    'MM': padZero(this.getMonth() + 1),    'DD': padZero(this.getDate()),    'HH': padZero(this.getHours()),    'mm': padZero(this.getMinutes()),    'ss': padZero(this.getSeconds())  };  return fmt.replace(/YYYY|MM|DD|HH|mm|ss/g, (key) => map[key]);};/** * Replace string variable * @param {Record} replacements * @returns {string} * @example  * 'Hello AAAnameBBB'.replaceVariable({name: 'World'},'AAA','BBB') // 'Hello World' */String.prototype.replaceVariable = function(replacements, prefix = '%#', suffix = '#%') {  let current = this.toString();  prefix = escapeRegex(prefix);  suffix = escapeRegex(suffix);  const seen = new Set();  const patterns = Object.keys(replacements).map(key => {    const escKey = escapeRegex(key);    return {      value: replacements[key],      placeholderRegex: new RegExp(        `${prefix}${escKey}(?=(?::.*?${suffix}|${suffix}))(?::.*?)?${suffix}`,        "gs"      ),      placeholderFormatRegex: new RegExp(        `(?
您需要登录后才可以回帖 登录 | 立即注册