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");
0

1 Answer 1

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'), '')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.