const fdChar = (string, char) => {
  let regex = /char/gi; 
  console.log(string.match(regex)); 
}
fdChar('bacon', 'a'); 

Hello everyone,

New to coding. Reintroducing myself to regular expressions. The above function is just a test for what will be implemented into a larger piece of code for a JavaScript coding challenge. I keep receiving null in the console. I want to target certain characters in a string. Am I not able to store the argument value in my regex variable?

function call fdChar(‘bacon’, ‘a’);

expected console output: ‘a’;

received output: null

2

try

    const fdChar = (string, char) => {
     let regex = new RegExp(char, 'gi');
      console.log(string.match(regex));
     }

     fdChar('bacon', 'a');

Here are the changes made:

Instead of /char/gi, I used new RegExp(char, ‘gi’) to dynamically create a regular expression based on the char parameter.

The g flag in the regular expression stands for global matching (i.e., find all matches rather than stopping after the first match), and the i flag stands for case-insensitive matching.

0