The RegExp object is a way to construct regular expressions. Neither of your examples is proper usage, but the first one probably works because the findHim var is a regular expression match result object, and it’s getting coerced to a string (which would == him) in this case.
There problem is “\s(him)[\s|.” will match "him " or “him.”
Thus, this sentence “Her mother raised him.” will turn into “Her mother raised him” – period at the end will be gone. I need “$1”, which in this case “him” to be replaced.
I also need the “g” global to be there to replace all instances.
I guess a solution could be to run two separate replacements?
draft.content = draft.content.replace(/ him /g, ' her ');
draft.content = draft.content.replace(/ him./g, ' her.');
My full “gender change” script looks like this:
// do the replacement…
draft.content = draft.content.replace(/Mr./g, 'Ms.');
draft.content = draft.content.replace(/He /g, 'She ');
draft.content = draft.content.replace(/ he /g, ' she ');
draft.content = draft.content.replace(/His /g, 'Her ');
draft.content = draft.content.replace(/ his /g, ' her ');
draft.content = draft.content.replace(/ him /g, ' her ');
draft.content = draft.content.replace(/ him./g, ' her.');
draft.content = draft.content.replace(/himself/g, 'herself');
// Update content
draft.update();