Is it possible to use a variable that contains regex in the search and replace action?
When I set local Replace
to something like \n
, the action replaces the find term with the literal "\n" instead of a new line.
I appreciate any suggestions!
Is it possible to use a variable that contains regex in the search and replace action?
When I set local Replace
to something like \n
, the action replaces the find term with the literal "\n" instead of a new line.
I appreciate any suggestions!
That's odd. When I do what you are doing, it doesn't insert "\n" it inserts a newline.
Therefore I suggest you upload your macro so we can spot the problem area.
You might want to specify your KM version and macOS version in case that's an issue.
For background and a possible solution to your problem have a look at this;
Hi, @Macs_Productivity. As @Airy reported, I don't seem to see the problem you are observing. Here's my test macro...
Download: _Test NewLine with RegEx.kmmacros (5.2 KB)
The replacement field is a token field. It is not processed by the regular expression system, so nothing related to the ICU Core regular expression characters applies to the replacement text. So the meaning of \n
is not that of the regular expression, instead it is that of the Keyboard Maestro Token processing.
Keyboard Maestro token fields process (by default) tokens and backslash characters, but regular expression fields (like the first one) do not process backslash characters because they are handled by the regular expression system.
So, in the replacement field, with Keyboard Maestro processing tokens and backslash characters, if you put a token (eg %Variable%Local Replace%
) or a backslash character (eg \n
), then they will be processed - but this processing does not happen recursively - results from the first round of processing are not then reprocessed.
So if you want a return character in the result, then the Local Replace had better have a return character in it, not \n
.
The question is then, how are you setting the Local Replace
variable. Because, by default, if you used the Set Variable to Text action, and it had \n
, that would be interpreted at that point as a return character and the variable would contain a return character.
If you are getting it from somewhere else, and you want to the add an extra level of processing, you could use the Filter action with the Process Tokens option. But note then it has to be a proper token text. If you, for example, had the text with percentage characters in there, then they should be doubled.
Here is the full version of my macro. The variable is being set with a "prompt for user input" action. Adding a "Filter" action with the Process Tokens option solved it.
Actions (v11.0.3)
Keyboard Maestro Actions.kmactions (2.3 KB)