You’re defining a regex like this.
const findRegex = /([M][r-s]\.\s)(\w*\s)(\w*)/g;
But you don’t use it anywhere.
You define a constant xx which isn’t a regex expression.
const XX = "XX";
You’re using that in your replace instruction.
Replacing “all instances of ‘XX’ with the group ‘$3’ from the regex” doesn’t really make any sense to me from a regex point of view.
From your initial text and result, it looks like you want to replace an instance of the text “XX” with the text “$3”. No regex group.
If you want to replace all instances, then a line like this would suffice.
draft.content = draft.content.replace(/XX/g, "$3");
But I’m not wholly convinced that is what you are really asking here looking at the sorts of terms being used. Based on that I’m going to go a bit further and question what it is you actually want to do…
If you want to find an instance of a salutation forename and surname and then pick out the surname as the third group, something like your const findRegex
would find a match when you run it; but as it stands that’s defined for global, so how would you know which instance of the name pattern to use?
I think what you are probably looking to do is find the first instance of a salutation, forename, surname, get the surname from that, and then do a find and replace across all instances of the text “XX” in your draft, replacing that with the surname.
Could you clarify what it is is you are trying to do?