Using regex with Search and Replace challenge

@camperextraordinaire but if i want to check dinamically some string regex with some matching patterns…is legit coding something like in this way?

  let text = "string example";
  let varToCheck = text;
  const regex = new RegExp**("\b"+varToCheck+"\b", "i");
  console.log(regex.test(text));

re-edit
example here i’d want to find a relative word that i passing in my function but return always false…is the correct way?

function myReplace(str, toFind, ) {
 let text = str;
 let varToCheck = toFind;
 const regex = new RegExp('\b'+varToCheck+'\b', "gi");
 console.log(regex.test(text));
 //return str;
 }
myReplace("Lorem ipsum dolor sit amet, consectetur adipiscing elit, ", "adipiscing");

@camperextraordinaire i’ve got this challenge : Search and Display
Definetely it works!..but i’d like to know if i could get the same result with regex.
in particular instead of declare a ternary with

let afterCapitalized =  before[0]===before[0].toUpperCase() ? 
  after.charAt(0).toUpperCase() + after.slice(1) : after ;

to check i there’s a first capital lecter…can i put it directly in my pattern regex something like :

const regex = new RegExp(‘\b’+varToCheck+‘\b’+(^spelngi|^store), “gi”);

this is my exercise solution :

function myReplace(str, before,after) {
  let text = str;
  let varToCheck = before;
  let afterCapitalized =  before[0]===before[0].toUpperCase() ? 
    after.charAt(0).toUpperCase() + after.slice(1) : after ;
  const regex = new RegExp('\\b'+varToCheck+'\\b', "gi");
      if(regex.test(text)){
      let texModified=text.replace(regex,afterCapitalized);
     return texModified;
  }
 }myReplace("Let us get back to more Coding", "Coding", "algorithms");

Hello @camperextraordinaire
yes…as previously i’ve mentioned, this code regex patterns :

const regex = new RegExp(’\b’+varToCheck+’\b’+(^spelngi|^store), “gi”);
assures me that any variable before get the capital lecter but not for spelngi and store?

@camperextraordinaire I want to state : Search in my string any words except “spelngi” and “store”

here

+(^spelngi|^store)

with + sign not concatenate the last piece of pattern? the anchor ^ inside the parenthesis () don’t meaning “not”?I 'd want to state in this parte, without use statement condition, that these words have not to be included in my regex pattern without to use these part of code

let afterCapitalized = before[0]===before[0].toUpperCase() ?
after.charAt(0).toUpperCase() + after.slice(1) : after ;

i mean this part is ok but i think it’s not a really god way for resolve the exercise because is clear that these type of exercise prefer the use of regular expression.

@camperextraordinaire sorry but an example of another topic where another user suggest for example to write something like [^abcd]… sorry in this case the anchor ^ is not meaning “No” ? if yes…a negation of ^ i can write only if i’m in a situation with only the parenthesis grouping class? i.e [^abcd];