How to match brackets to a regular expression in ruby

I'm currently trying to create a regex that will find this pattern /- - [/ . However, since [ is a special character, I get this error:

 premature end of char-class: /- - [/ (SyntaxError) 

I know this happens because the compiler expects [ to regex to close, but I need it to match this in my template. How can i do this?

+4
source share
1 answer

Yes, brackets are part of the regex syntax. If you want to match a literal bracket (or any other special character, for that matter), run with a backslash.

 'foo[bar]' =~ /\[/ # => 3 
+3
source

Source: https://habr.com/ru/post/1487495/


All Articles