如何使用字符串变量创建JavaScript正则表达式?

是的,请使用 新的RegExp(pattern,flags)在JavaScript中实现此目的。您可以尝试运行以下代码以使用字符串变量实现JavaScript regex-

<!DOCTYPE html>
<html>
   <body>
      <script>
         var str = 'HelloWorld'.replace( new RegExp('hello', 'i'), '' );
         document.write(str);
      </script>
   </body>
</html>