0

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 /[...]/?

1 Answer 1

1

The RegExp constructor takes a string, so you can do string things with it:

var pattern = new RegExp("["+this.expression.join('')+"]","g");
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, word. Is there no parameter for this in the constructor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.