3

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); 

Here's the regexr and fiddle.

What did I do wrong?

4
  • 3
    If you print the source string, you'll see there are no backslashes in it. You forgot to escape the backslashes in the string literal, so \a is treated as a Bel character, \b becomes a backspace, and \O is just O. Commented Nov 14, 2015 at 5:39
  • 1
    Check This Commented Nov 14, 2015 at 5:44
  • Ohhh I get it now... feels convoluted to edit the string to escape all the backslash just to get my regex to work. I guess in cases like these you would just have to avoid looking using the backslash as part of your regex and build it some other way? Commented Nov 14, 2015 at 7:08
  • 1
    If I could offer an optimization to using .*?, 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. Commented Nov 14, 2015 at 8:17

1 Answer 1

3

I need to match all the stuff between an underscore _ and a backslash.

Your regex /_(.*?)(?=\\)/g does not really do that as it will stumble on a newline.

To actually match anything between an underscore and a backslash, you need a negated character class [^\\] - any character but a backslash.

Here:

/_([^\\]*)/g

This regex matches:

  • _ - matches an underscore
  • ([^\\]*) - 0 or more characters other than a backslash.

JS sample snippets:

var str = 'IShouldGet3Match,allTheSequenceBetweenaUnderScoreAndASlashHeresOne:_1234_1234\\andheresAnother_1234_1234_1234\\OhLookAnotherOne!_123_12_1234567\\bahbahba';

var testPattern = /123/g;
var testResult = str.match(testPattern);
document.getElementById("demo").innerHTML = testResult;

var pattern = /_([^\\]*)/g;
var result = str.match(pattern);
document.getElementById("demo2").innerHTML = result;

var pattern = /_([^\\]*)/g;
while ((m=pattern.exec(str)) !== null) {
  document.getElementById("demo3").innerHTML += m[1]+"<br/>";
}
<body>
    <p>Just making a basic regex works with 123:</p>
    <p id="demo"></p>
    <p>Result of actual Regex:</p>
    <p id="demo2"></p>
    <p>Captured texts of actual Regex:</p>
    <p id="demo3"></p>
</body>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.