So, I did a global undoable RegEx search and replace. I forgot to include the '
in the replace. Now I need to do a search on strings that match the below. It must not start with a '
and will have | translate
at the end. These are Angular translation keys - they can be all over in the template file (HTML). They always begin with {{, have | translate, and end with }}. Now the kicker is they could have spacing or line break issues (less likely but a chance). So it could be {{_ _ textToKeepAdd'To _ _ | _ _ translate _ _ }} The _ _ being spaces or a line break possibility.
Strings To Match (no beginning '):
anyText' | translate
<other text or tags>{{ anyText' | translate
{{ // line break
anyText' | translate
anyText'
| translate // line break
Strings Not To Match:
'anyText' | translate
<other text or tags>{{ 'anyText' | translate
'anyText'
| translate
Return String Format:
'anyText' | translate
Example:
blahadskfjlksjdf' | translate = 'blahadskfjlksjdf' | translate
'SkipMe' | translate = not found for replacement bc it starts with a '.
And <other text or tags>{{ anyText' | translate = <other text or tags>{{ 'anyText' | translate
Here is the code that I biffed on - '(?:\w+\.){1,3}(?=\w+'\s+\|\s+translate\b)
I am going to need a group capturing/returning in the replace.
This should do the trick:
Replace \{\{(?:\s|\n)*(?!(?:'|\s|\n))(.*')(?:\s|\n)*(\|(?:\s|\n)+translate)\b
with {{ '$1 $2
Regex 101 Demo
Explanation:
\{\{
- match two open braces
(?:\s|\n)*
- match any number of whitespace characters
(?!(?:'|\s|\n))(.*')
- capture group 1; match any continuous string of not '
characters followed by a single '
(?:\s|\n)*
- match any number of whitespace characters
(\|(?:\s|\n)+translate)
- capture group 2; match |
followed by at least one, or more, whitespace character and then the word translate
.
\b
- match a word boundary
I suggest using
Find What: \{\{[\s\n]*(?!['\s\n])(.*')[\s\n]*(\|[\s\n]+translate)\b
Replace With: {{ '$1 $2
See online regex demo (altered to reflect how it works in VSCode).
Details
^
- start of a line
\{\{
- a {{
substring
[\s\n]*
- 0+ whitespaces/linebreaks
(?!['\s\n])
- a negative lookahead failing the match if immediately to the right of the current location there is a '
or a whitespace (linebreak included)
(.*')
- Capturing group 1: any 0+ chars other than line break chars, as many as possible and then a '
char
[\s\n]*
- 0+ whitespaces/linebreaks
(\|[\s\n]+translate)\b
- Group 2: a |
, 1+ whitespaces/linebreaks and a whole word translate
.
The replacement is '
, Group 1 backreference (referring to the value captured in Group 1), space and Group 2 backreference (referring to the value captured in Group 2).