--- warning: This file is auto generated by `yarn web:inject`, do not edit by hand examples: - trimstart(' hello ') - trimstart('__hello__', '_') returns: - '''hello ''' - '''hello__''' dependencies: [] authors: original by: - Kevin van Zonneveld (https://kvz.io) notes: [] parityVerified: - PowerShell 7.4 isTypescript: true type: function layout: function title: PowerShell's string.trimstart in TypeScript description: >- Here's what our current TypeScript equivalent to PowerShell's string.trimstart looks like. function: trimstart category: string language: powershell permalink: powershell/string/trimstart/ alias: - /functions/powershell/trimstart/ - /functions/string/trimstart/ - /powershell/trimstart/ ---
{% codeblock lang:typescript %}function escapeForCharClass(chars: string): string { return chars.replace(/[\\\]^/-]/g, '\\$&') } export function trimstart(str: string, chars?: string): string { // discuss at: https://locutus.io/powershell/trimstart/ // parity verified: PowerShell 7.4 // original by: Kevin van Zonneveld (https://kvz.io) // example 1: trimstart(' hello ') // returns 1: 'hello ' // example 2: trimstart('__hello__', '_') // returns 2: 'hello__' const value = String(str) if (chars === undefined) { return value.trimStart() } if (chars === '') { return value } return value.replace(new RegExp(`^[${escapeForCharClass(chars)}]+`, 'g'), '') } {% endcodeblock %}