Using regex with Search and Replace challenge

@RandellDawson 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");

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");
 //return str;
myReplace("Lorem ipsum dolor sit amet, consectetur adipiscing elit, ", "adipiscing");

Not sure why you are using the same string for the varToCheck as the string you are testing (text), but maybe you have your reasons. If you are wanting the console.log statement to display true, then you will need to escape the “” in the string, like:

const regex = new RegExp**("\\b"+varToCheck+"\\b", "i");

@RandellDawson 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");
      let texModified=text.replace(regex,afterCapitalized);
     return texModified;
 }myReplace("Let us get back to more Coding", "Coding", "algorithms");

Seems like you have figured out the solution. If you have any further questions, just let us know.

Hello @RandellDawson
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?

What happens when you try the above regex? My guess is it errors out. When you use the new RegExp syntax, the regex must be supplied as a string.

Adding the above would do nothing for you. The ^ is related to the start of a string which is not relevant here. I am really not understanding what you are trying to accomplish here.

@RandellDawson I want to state : Search in my string any words except “spelngi” and "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.



You are using a regular expression to solve the problem. The issue is you need a way to know if the first letter of before is uppercase or not. This will determine if you need to change the case of the first letter of after. This extra bit of logic is needed and used to create the replacement value for the replace method’s second argument. I can not think of a way to only use regex without this extra piece of logic to solve this problem.

@RandellDawson 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];

The ^ used at the start of a character class [^abc] means not one of the characters a, b, or c.