RegExp: find the square bracket "[" if the word "foo" is not in front of it?

bar[ <--- это найти foo[ <--- а это не надо 

Thank!

  • In js regex, there is no lookbehind, and without this, with a single expression you cannot correctly make a match - zb '

1 answer 1

The regular expression will be like this

 ^(?!foo).*\[ 

explain

On JS will be like this

 var regexp = new RegExp("^(?!foo).*\\["); var string = 'bar['; console.log(string.search(regexp)); 
  • var string = 'afoo['; - zb '
  • @zb '- It seems that the description says, find the square bracket "[" if the word "foo" is not in front of it? . If you have a working solution , discard it - Farkhod Daniyarov
  • One regexp on js in any way, you need a backreference - zb '
  • In fact, in such cases it is always important to know the source data and why to look for it - zb '
  • @FarkhodDaniyarov why not so "(? <! Foo) [" you decided to write? I think this is more correct - Ilya Babushkin