Question
I'm trying to match PowerShell dash comments (# ...) but not inline comments (<# .. #>) in same regex. How can I achieve it?
Goal
Match
I'd like to match PowerShell comments (using hashtag comment syntax). So simply everything after # is commented out. I use #(.*$)/gm for it.
Test-cases where the regex match is written inside brackets [..]:
Write-Host "Hello world" [# comment here][# A line with only comment]Comment without whitespace[#before][Comment with whitespace [#after ]
Do not match
However what I'd like to use here is have an exception for "inline comments syntax". Inline comments in PowerShell looks like lorem <# inline comment #> ipsus.
So here I'm looking for exclusions for:
Write-Host "Hello world" <# inline comment here #><# A line with only inline comment #>Comment without whitespace<#no whitespace#>aroundInline comment <# in middle #> of lineComment with whitespace #comment with >Comment with whitespace #comment with <Comment with whitespace #comment with <# test #>
What I tried
I tried to use [^<>] for something like #[^<>](.*[^<>]$) but it did not work for all cases given in the above.
My progress on regex101 until I got stuck.
Why
I'm parsing PowerShell in JavaScript/TypeScript runtime to be able to inline them to run them in batch (cmd) for a community driven open-source project. I know there will be exceptions to this (like strings with dashes inside) but I trade off simple regex parsing for robustness.
Thank you!