简体   繁体   中英

RegExp character class with RegExp constructor JavaScript

I am building a simple RegExp helper for my own personal use. Essentially, it's going to work with method chaining and return the string with all of the selected elements.

The question I have is how to use a character class with the RegExp constructor. Here is my code:

var pattern = new RegExp(this.expression.join(''), "g");

As you can see, I am passing my expression in the first argument and a flag in the second.

My question is, how can I use the RegExp constructor and wrap it in a character class, like /[...]/ ?

RegExp构造函数采用字符串,因此您可以使用它来做字符串事情:

var pattern = new RegExp("["+this.expression.join('')+"]","g");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM