in Programming by (13.5k points) edited by
0 like 0 dislike

How I can to regular expression pattern concatenate with variable in JavaScript programming language?

javascript regex concat variable

1 Answer

0 like 0 dislike
by (13.5k points)

In JavaScript you can create regular expression variable value concatenate with variable only with help RegExp object constructor. No other way.

Use RegExp object constructor.

var re = new RegExp('pattern', 'flags');

In pattern write plain string without slashes:

var word = 'foo';
var regex = new RegExp('(' + word  + ')', 'gi');

You always can check containts regular expression variable. Just call it in js console. If variable value show witout quotes and template is framed by slashes, then this is right regular expression variable value.

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.