fast-replaceall
Version:
Supports global replacement, case-insensitive mode, start index control, and functional replacement compatible with native `String.replace`.
22 lines (21 loc) • 750 B
TypeScript
export interface ReplaceAllOptions {
fromIndex?: number;
caseInsensitive?: boolean;
}
export type ReplacementFn = (match: string, offset: number, str: string) => string;
/**
* 字符串替换函数
*
* @example
* ```js
* replaceAll('这是一个字符串', 'dog', 'monkey'); // '这是一个字符串'
* replaceAll('这是一个字符串', '一', (match, offset) => {}); // '这是undefined个字符串'
* ```
*
* @param str 字符串
* @param substr 子字符串
* @param replacement 替换内容或函数
* @param options 选项
* @returns 新字符串
*/
export default function replaceAll(str: string, substr: string, replacement: ReplacementFn | any, options?: ReplaceAllOptions): string;