t-comm
Version:
专业、稳定、纯粹的工具库
65 lines (63 loc) • 1.07 kB
JavaScript
/**
* 移除第一个反斜杠
*
* @export
* @param {string} [str=''] 输入字符串
* @returns {string} 字符串
* @example
* ```ts
* removeFirstSlash('/abc/ddd/')
*
* 'abc/ddd/'
* ```
*/
function removeFirstSlash(str) {
if (str === void 0) {
str = '';
}
if (str.startsWith('/')) {
return str.slice(1);
}
return str;
}
/**
* 移除最后一个反斜杠
*
* @export
* @param {string} [str=''] 输入字符串
* @returns {string} 字符串
*
* @example
* ```ts
* removeLastSlash('/abc/')
*
* '/abc'
* ```
*/
function removeLastSlash(str) {
if (str === void 0) {
str = '';
}
return str.replace(/\/$/, '');
}
/**
*移除第一个和最后一个反斜杠
*
* @export
* @param {string} [str=''] 输入字符串
* @returns {string} 字符串
*
* @example
* ```ts
* removeFirstAndLastSlash('/abc/')
*
* 'abc'
* ```
*/
function removeFirstAndLastSlash(str) {
if (str === void 0) {
str = '';
}
return str.replace(/^\/|\/$/g, '');
}
export { removeFirstAndLastSlash, removeFirstSlash, removeLastSlash };