I am trying to use following nodejs function to check an existence of specific kind of url in my string. I checked this regex separately and it looks good, but get me an error in node.
var checkforURL = function(data) {
var re = /((?:https?://)?www\.example\.com\/tp\/\S+\d+)/g/;
return re.test(data);
}
this should return true for following cases
etc etc
when i try to run the code i get following compilation error var re = /((?:https?://)?www.example.com/tp/\S+\d+)/g/; ^
SyntaxError: Unexpected token ) at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3
I need this parenthesis, they enclose https?:// to make sure it can exist 0 or 1 time, not sure why nodejs keeps throwing an error, any idea?
You need to escape the forward slashes in https?://
and also you need to remove the last /
after g
. Change the string example
to exmaple
,
var re = /((?:https?:\/\/)?www\.exmaple\.com\/tp\/\S+\d+)/g;
The above regex should match the following urls,
http://www.exmaple.com/tp/123
www.exmaple.com/tp/abc/123
https://www.exmaple.com/tp/123
To match example
links,
var re = /((?:https?:\/\/)?www\.example\.com\/tp\/\S+\d+)/g;
If you want to match both example and exmaple links then give m
and a
inside a character class.
((?:https?:\/\/)?www\.ex[ma][ma]ple\.com\/tp\/\S+\d+)