0 like 0 dislike
29 views
asked in Programming by (3.7k points)
edited by

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

javascript regex concat variable

Your answer

pre, code

Preview

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 (3.7k 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 2 views
0 like 0 dislike
1 answer 8 views
0 like 0 dislike
1 answer 21 views
0 like 0 dislike
1 answer 13 views
0 like 0 dislike
1 answer 4 views
0 like 0 dislike
1 answer 6 views
...