JavaScript regex engine did not support look-behinds at all before ECMAScript 2018 was released.
Now, if you use this in Chrome, it will not throw any error now:
var link = "www.google.com";
var reg = '^'+link+'{1}|(?<=\s)'+link+'{1}(?=\s)|'+link+'{1}$';
console.log(reg);
var result = new RegExp(reg, 'g');
Another thing: you must double escape \ inside RegExp constructor.
What you are trying to acheive is to make the URL match at word boundaries.
Try using
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
var reg = '\\b'+RegExp.escape(link)+'\\b';
Code:
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
var link = "www.google.com"
var reg = '\\b'+RegExp.escape(link)+'\\b';
alert(new RegExp(reg, "g"));
Note I am adding the RegExp.Escape in order to escape special characters in the arguments passed to the RegExp constructor (e.g. . must be \\.).
?<=