1

I have the following code that checks a URL if it contains a certain pattern:

var url = "https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=regex%20javascript";
var patt = new RegExp('https?:\/\/[^/]+\.google\.[a-z.]+\/((search[?#])|(webhp[?#])|([?#])).*q=');
var check = patt.test(url);
alert(check);

The above regex won't work without using new RegExp(). How do I use the same regex without creating the regex object. For example, something like this (which doesn't seem to work):

var url = "https://www.google.co.uk/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=regex%20javascript";
var check = ('https?:\/\/[^/]+\.google\.[a-z.]+\/((search[?#])|(webhp[?#])|([?#])).*q=').test(url);
alert(check);
1
  • 3
    What makes you think you can do it without creating a regex object? And why do you want to do that? Commented Jul 31, 2016 at 11:36

1 Answer 1

1

Do you mean a regex literal like so

var check = /https?:\/\/[^/]+\.google\.[a-z.]+\/((search[?#])|(webhp[?#])|([?#])).*q=/.test(url)  

this is however merely syntactic sugar and does not free you of creating actual RegEx objects

Sign up to request clarification or add additional context in comments.

3 Comments

That's a regex object as well.
I know but maybe that is what the questioner meant - I could not imagine any other meaning :) Of course my guess could be wrong
Yeah that's why it would be interesting if the OP would answer the question in my comment. :)

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.