Function Parameter in Regexp

Hi!

I was doing exercises and this doubt popped up. I have searched on the internet but I can find none. My conclusion is that it is not possible, but still, I have my doubts.

Can a function parameter be used in a Regexp?

For example:

function search(string, searched) {
var regexp = /searched/;
return string.regexp;
}

The string parameter should be a string given by the user. And the searched parameter is the word, or maybe a letter, that it is being searched.

i googled your question and there were multiple resources out there on how to do this including
http://janetriley.net/2014/12/javascript-regular-expression-with-a-variable.html

2 Likes