0

As you can see below, I'm trying to count how many times a character in string J occurs in string S. The only issue is I can't put the argument o in the forEach loop into the regex expression as shown in the console.log.

var numJewelsInStones = function(J, S) {
    let jArr = J.split('');
    let sArr = S.split('');

    jArr.forEach(o=>{
    console.log(S.replace(/[^o]/g,"").length);      
    })
};

numJewelsInStones("aA", "aAAbbbb");
user10109
  • 121
  • 1
  • 3
  • 9

1 Answers1

0

You can create regular expression with constructor function where you pass string parameters:

new RegExp('[^' + o + ']', 'g')

Your replace logic might look like:

S.replace(new RegExp('[^' + o + ']', 'g'), '')
madox2
  • 49,493
  • 17
  • 99
  • 99