I am trying to split the string based on two separators like this:
"some-str_to_split".split(/-|_/)
So it should split the string based on "-" and "_". It works fine, but Webstorming giving a warning:
Single character alternation in regex
Note that -|_
is a patter that matches -
or _
, but each time the regex engine sumbles upon a position in the string, the -
is tested against first, and if it is not matched, _
is tried. That involves the so-called backtracking. When you use [-_]
, a so called character class, a regex engine uses a compiled mini-program that performs the search faster eliminating the backtracking mechanism.
Thus, the warning is just a kind of a hint for you that your pattern can be enhanced. In case there are 2 chars, the difference in performance is negligeable, if there are 100+ alternatives, the difference will be more tangible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With