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

javascriptobject oriented programmingfront end technology更新于 2024/6/26 4:28:17

是的,使用 new RegExp(pattern, flags)在 JavaScript 中实现此目的。您可以尝试运行以下代码,使用字符串变量 − 实现 JavaScript 正则表达式

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

相关文章


有用资源