underscore
underscore

Reputation: 6887

Ingore word and put underscore on each word Regex

I just need to find and replace the following word set in VsCode

t("US.TL.PACKAGES:installmentDetail.discount")

I was tried a lot by following

(?:US\.TL\.PACKAGES:)([a-z])([A-Z])

but when I used

([a-z])([A-Z])

this captures all upper in the string.

I need to group by each new word.

installment Detail discount

To put underscore i'm using

\1_\2 in Replace

Final output

US.TL.PACKAGES:INSTALLMENT_DETAILS.DISCOUNT

Upvotes: 3

Views: 155

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627327

The following can be used in the Search & Replace tool, not in the Find/Replace in Files feature (opened with Ctrl+Shift+F) because the latter uses another, old regex engine and the former uses the modern ECMAScript 2018+ compliant engine as in most major JavaScript environments:

  1. Insert an underscore between each lower- and uppercase letter in a non-whitespace streak of text after a US.TL.PACKAGES: string:
(?<=\bUS\.TL\.PACKAGES:\S*?[a-z])(?=[A-Z])

Replace with a mere _. Make sure the Aa option is checked as matching must be case sensitive:

enter image description here

  1. Make all streaks of letters in a non-whitespace streak of text after a US.TL.PACKAGES: string uppercase:
(?<=\bUS\.TL\.PACKAGES:\S*?)[A-Za-z]+

Replace with \U$0.

enter image description here

and the result is

enter image description here

Upvotes: 2

Related Questions