0 like 0 dislike
42 views
asked in Programming by (5.8k points)
edited by

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

javascript regex concat variable

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.

1 Answer

0 like 0 dislike
answered by (5.8k 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.

Related questions

0 like 0 dislike
1 answer 23 views
0 like 0 dislike
1 answer 27 views
0 like 0 dislike
1 answer 25 views
0 like 0 dislike
1 answer 35 views
0 like 0 dislike
1 answer 16 views
0 like 0 dislike
1 answer 24 views
0 like 0 dislike
1 answer 24 views
0 like 0 dislike
1 answer 16 views
0 like 0 dislike
1 answer 16 views
...