Here is a code snippet that checks a URL for a specific 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 regex used above requires the use of new RegExp()
. How can I achieve the same result without creating a new regex object? For example, I attempted the following code (which turned out to be incorrect):
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);