Replace text but not if contain specific characters?

In JavaScript, I am using the below code to replace text that matches a certain string. The replacement wraps the string like this: "A(hello)". It works great but if there are two strings that are the same, for example: "Hello hi Hello", only the first one will get marked and if I am trying twice, it will get marked double, like this: "A(A(Hello)) Hi Hello".

A solution to this could be to not replace a word if it contains "A(" or is between "A(" and ")"; both would work.

Any idea how it can be achieved?

Note: I cant use replaceAll because if there is already a word that is replaced and a new word is added, then the first one will be overwritten. Therefore I need a solution like above. For example,If I have a string saying “Hello hi”, and I mark Hello, it will say “A(Hello) hi”, but if I then add Hello again to the text and replace it, it will look like this: A(A(Hello)) hi A(Hello).

Here is what I got so far:

let text = "Hello hi Hello!"
let selection = "Hello"
let A = `A(${selection})`
let addWoman = text.replace(selection, A)

Answer

You can use a negative lookahead assertion in your pattern that fails the match if we A( before full word Hello:

(?<!A()bHellob

And replace it with A($&)

RegEx Demo

Code:

let text = "Hello hi Hello!";
let selection = "Hello";
let A = `A(${selection})`;
let re = new RegExp(`(?<!A\()\b${selection}\b`, "g");
let addWoman = text.replace(re, A);

console.log(addWoman);

console.log(addWoman.replace(re, A));