I want to replace any special characters and spaces in a string with hyphen. Below is my code:
$c = 'This_is my code !@# characters are not $ allowed% remove spaces ^&*(){}[]/_:;,.?/"'''
$c = $c -replace [regex]::Escape('!@#$%^&*(){}[]/:;,.?/"'),('-')
Write-Host $c
Is there any direct way to find all special characters, spaces and replace with a single character hyphen
\W will replace any non word character. it will not replace a-z, A-Z, 0-9
$c = 'This_is my code !@# characters are not $ allowed% remove spaces ^&*(){}[]/_:;,.?/"'''
$c -replace '\W','-'
This_is-my-code-----characters-are-not---allowed--remove-spaces-----------_--------