-3

PLEASE STOP MARKING AS DUPLICATE QUESTION BEFORE YOU POST RIGHT ANSWER FOR ME. This post is not a place where someone is show off his ROUGH search skills on google.

Simple reg exp is not a problem to inject js variable. but what about the complex one like following?

I have a reg expression to check if the URL is from example.com domain.

^(https?:\/\/(.+?\.)?example\.com(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)

Now I want to create a function isValidDomainURL(url, domain) and inside of the function I d like to use above expression replaced example\.com with domain

How can I create a reg expression using js variable inside of it?

Codemole
  • 2,649
  • 4
  • 19
  • 40
  • 4
    Try googling your exact question title next time. [Javascript Regex: How to put a variable inside a regular expression?](http://stackoverflow.com/questions/4029109/javascript-regex-how-to-put-a-variable-inside-a-regular-expression) – CollinD Oct 14 '15 at 18:50
  • With all due respect, I think you're confused about how things work. If a question is a duplicate, then there is no need to answer it; the answer is already there, in the duplicated question. In this particular case, your question is an **exact** duplicate. Please don't post duplicate questions. At best they will be closed; at worst, they will be downvoted, hurting your reputation. Search for the answer before posting. I guarantee you, you are not the first person in the world trying to figure out how to insert a dynamic part into a regexp. –  Oct 14 '15 at 19:42
  • You also seem confused about downvoting. Downvoting simply means "the answer is not useful". It does not require the downvoter to try to answer the question himself, or even to explain himself. If an answer is wrong--meaning it doesn't work, or doesn't solve the problem--that is certainly "not useful" and justifies a downvote. In this case the example posted generated a JS syntax error. That certainly seems "not useful" to me. –  Oct 14 '15 at 19:45
  • @torazaburo I was not sure when reg exp is so complex, how to add variable. no point anyway as I found the right answer thank to **OceansOnPluto**. Very much appreciated for **OceansOnPluto**'s help. – Codemole Oct 14 '15 at 20:19

1 Answers1

1

You can use the RegExp constructor.

var theVariable = "Hello"

var theRegex = new RegExp("." + theVariable + ".");

With your particular URL, it would be something like the following.

var domain = "somedomain.com";
var theRegex = new RegExp("^(https?:\/\/(.+?\\.)?" + domain + "(\/[A-Za-z0-9\\-\\._~:\/\\?#\\[\\]@!$&'\(\)\*\+,;\\=]*)?)")

And then you can use that regex anywhere, or create a new one with a different domain when you want.

whatoncewaslost
  • 2,027
  • 15
  • 22
  • 1
    The method works but I wrote a bad regular expression for it. I'll edit it. – whatoncewaslost Oct 14 '15 at 18:57
  • I have actually tried such a thing, getting the error message saying nothing to repeat, so I posted the question here. So sad to see someone to vote down even not trying or giving any good answer... btw, can you kindly check your new RegExp and give me a correct one? – Codemole Oct 14 '15 at 18:58
  • 1
    Done! You need to double the backslashes in your string so the code reading the string doesn't use it as an escape character for the NEXT character. – whatoncewaslost Oct 14 '15 at 19:03
  • so what is the correct one? I have copied and tried your regexp but it still shows error... :-( – Codemole Oct 14 '15 at 19:09
  • Thanks. It works. :D – Codemole Oct 14 '15 at 19:29
  • 1
    Actually not, since it would also match `somedomainXcom`. –  Oct 14 '15 at 19:40