I need to match all the stuff between an underscore _ and a backslash . So anything that's like bahbah_12345_12345\bahbah I want just the part that's _12345_12345.
I used regexr and regex101 to help me build what I want and looks like it works on, but when I put the same string and regular expression in my JS code it finds nothing.
For example here's my regex and sample string
Regex
_(.*?)(?=\\)
Sample String
IShouldGet3Match,allTheSequenceBetweenaUnderScoreAndASlashHeresOne:_1234_1234\andheresAnother_1234_1234_1234\OhLookAnotherOne!_123_12_1234567\bahbahba
On regex I get 3 matches, looks good. Then I put it in JS code and the regex doesn't match anything.
var pattern = /_(.*?)(?=\\)/g;
var result = str.match(pattern);
What did I do wrong?
\ais treated as a Bel character,\bbecomes a backspace, and\Ois justO..*?, try this:_([^\\]*)(?=\\)It will look for all characters after a_until it hits the backslash. If you think you will encounter newlines that you wish to avoid, you can always add that to the character group:_([^\n\\]*)(?=\\)example 1. This takes 73 steps to find 9 matches. Otherwise,.*?will take 937 steps example 2.