2

I am using this regex ^(?:foo|bar)+|(.+)$ to catch string different from 'foo' or 'bar' but it catches string only after foo or bar and not at beginning. For example it catches ba in string foobarfooba but doesn't catch ba in string bafoobarfoo.

Demo

1 Answers1

1

Because you used the start of a line anchor. Removing the start of the line anchor also won't work for you. So I suggest you to use the below regex.

var s = "bafoobar";
var re = /foo|bar|((?:(?!foo|bar).)+)/gm;
alert(re.exec(s)[1])

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274