6

I am creating a regex dynamically.

    var link = "www.google.com";
    var reg = '^'+link+'{1}|(?<=\s)'+link+'{1}(?=\s)|'+link+'{1}$';
    console.log(reg);
    var result = new RegExp(reg, 'g');

I am getting this error

Uncaught SyntaxError: Invalid regular expression: /^www.google.com{1}|(?<=s)www.google.com{1}(?=s)|www.google.com{1}$/: Invalid group

Here is the generated regex:

^www.google.com{1}|(?<=s)www.google.com{1}(?=s)|www.google.com{1}$

10
  • 1
    Did you use some non-JS syntax? For example I do not recognise ?<= Commented May 20, 2015 at 14:46
  • Why the close vote??? Commented May 20, 2015 at 14:47
  • It ensures that the given pattern will match, ending at the current position in the expression. Does not consume any characters. Ex : /(?<=foo)bar/ Match : foobar Commented May 20, 2015 at 14:49
  • It gives me a red light here: regexpal.com Commented May 20, 2015 at 14:51
  • 3
    JavaScript does not support lookbehind. There are various workarounds; I can not tell you which to use from the information you provide . Commented May 20, 2015 at 14:52

2 Answers 2

5

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 \\.).

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

4 Comments

is there any alternative for look-behind in javascript ??
i apreciate your help.. Thank you.
There are alternatives, but there is none in case you have undefined number of characters before and after the string you want to match.
Do you need more assistance with this, or is everything working as expected?
1

JavaScript does not support lookbehind groups.

On top of that, your regular expression is being built up from strings. You have to make sure that your regular expression metacharacters "survive" the process of string constant parsing, and in particular your \s needs to be expressed as \\s.

Note also that the . characters in the URL portion of your pattern will be interpreted as the regex "wildcard" symbol if you don't precede them with \\ as well.

Finally, it's not clear what you expect those {1} things to do; in JavaScript that will match the sequence of characters {1}.

4 Comments

{1} is a quantifier (albeit a useless one). /^m{1}$/.test('m') returns true (in Firefox at least).
@FelixKling oh durr. Not enough coffee yet :)
No worries :) It wouldn't have been that surprising to me if {1} was treated differently ;)
i apreciate your help.. Thank you.

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.