hldy_xcx/compontent/public/long.js

21 lines
819 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 将字符串中的 '9999-12-31' <-> '长期' 互换并返回修改后的字符串
* 同时存在两者时不会互相覆盖(使用占位符处理)
* @param {any} v - 期望传入 string非 string 将直接返回原值
* @returns {string|any} 修改后的字符串,或原值(非 string
*/
export function swapLongTerm(v) {
if (typeof v !== 'string') return v;
const PLACEHOLDER = '__$LONGTERM_PLACEHOLDER$__';
// 用来把任意字符串安全地放进 RegExp 构造器
function escapeRegExp(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
return v
.replace(/9999\.12\.31/g, PLACEHOLDER) // 注意转义点 \.
.replace(/长期/g, '9999.12.31')
.replace(new RegExp(escapeRegExp(PLACEHOLDER), 'g'), '长期'); // 安全转义占位符
}