I am trying to figure out how to use Find & Replace with RegEx to manipulate text. In this case, want to set a variable using RegEx and then replace text with the variable.
Text:
Mr. John Smith
Mr. XX goes to town.
Script
// define regex to use...
const findRegex = /([M][r-s]\.\s)(\w*\s)(\w*)/g;
// define replacement expression...
const FirstName = "$2";
const LastName = "$3";
//Find Text
const XX = "XX";
//
// do the replacement...
draft.content = draft.content.replace(XX, LastName);
draft.update();
Result:
Mr. John Smith
Mr. $3 goes to town.
What am I doing wrong?