UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

59 lines (57 loc) 1.19 kB
/** * 移除第一个反斜杠 * * @export * @param {string} [str=''] 输入字符串 * @returns {string} 字符串 * @example * ```ts * removeFirstSlash('/abc/ddd/') * * 'abc/ddd/' * ``` */ function removeFirstSlash() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; if (str.startsWith('/')) { return str.slice(1); } return str; } /** * 移除最后一个反斜杠 * * @export * @param {string} [str=''] 输入字符串 * @returns {string} 字符串 * * @example * ```ts * removeLastSlash('/abc/') * * '/abc' * ``` */ function removeLastSlash() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return str.replace(/\/$/, ''); } /** *移除第一个和最后一个反斜杠 * * @export * @param {string} [str=''] 输入字符串 * @returns {string} 字符串 * * @example * ```ts * removeFirstAndLastSlash('/abc/') * * 'abc' * ``` */ function removeFirstAndLastSlash() { var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return str.replace(/^\/|\/$/g, ''); } export { removeFirstAndLastSlash, removeFirstSlash, removeLastSlash };