0

Creating RegEx and combining it with variables that are strings with a certain pattern doesn't always work out well, if the pattern doesn't change to work with the string.

"\\" + ) = \) in RegEx. Which is an escaped ) and not the closing bracket of a capturing group anymore.

"\\\\" + ) = \\) in RegEx. Here \ successfully gets escaped and the capturing group does not break.

In a normal string or RegEx expression with // only, so no concatenation, this "\\" would be working fine as an escaped backslash \.

The problem is, in RegEx the \ is also used for something.

What I am wondering is, if there's a way to make something flexible. That would prevent a pattern like this to match , breaking when combined together. Example, matching example\.

"(" + "example\\" + ")"

This one when combined together breaks, because when used as a RegExp, the \ gets escaped, and the capturing group breaks.

To fix it, it would have to look like this

"(" + "example\\" + "\\)"

Here, \ one backlash gets escaped and then another one gets escaped, and together in RegEx, it would result into one escaped bracket \

because what I basically created was \\ when combining it all together.

The problem is, this breaks again, if there wouldn't be \\ infront of example.

 

Here is an example code.

To explain, the function matches the path after example_folder with strings in the matchingArray that end with a \. Because it's in a string, I have to do \\ so that it appears as a \ backslash.

class example_options {
    matchingArray = ["example\\", "example\\\\special\\", "something_else\\"]
}

function example(options) {
    if (typeof(options) != "object") {
        options = new example_options()
    }

    var test = `"example_folder\\example\\special\\test.txt"`

    for (let i=0; i < options.matchingArray.length; i++) {
        var regexPattern = new RegExp(`(?:example_folder\\\\)(${options.matchingArray[i]}\\)`)

        var match = test.match(regexPattern)
        if (match) {
            console.log(match, options.matchingArray[i])
        }
    }
}

example()

example() executes the function, if you would be running it, which successfully works.

But the problem is the flexibility of it.

I wouldn't have to backlash it twice, if the RegEx would have been constructed without using string, but the slashes /:

var regexPattern = new RegExp(/(?:example_folder\\)(example\\special\\)/)

matchingArray = ["example\\", "example\\\\special\\", "something_else\\"] here I had to add \\\\, because in RegEx a \ is also used for its own thing.

At the end of the expression here:

var regexPattern = new RegExp(`(?:example_folder\\\\)(${options.matchingArray[i]}\\)`)

I had to add a \\at the end.

This would mean that in RegEx it ends up like this \) and the capturing group gets destroyed. However, since I have backslashes in the matchingArray. When all of this gets combined at the end, the RegEx near the bracket looks like this \\) and the capturing group is preserved.

Is there a way to deal with this issue?

What if I wanted the matchingArray to be like this? To be compatible with other things that aren't RegEx.

matchingArray = ["example\\", "example\\special\\", "something_else\\"]

If I would do this, it wouldn't work with RegEx anymore, because this example\\special\\ would end up like this example\special\. Then the issue would be this \s.

The other issue is that, I have to add this at the end \\) of the RegEx expression. But I only need to do that, if I know that there is going to be a \\ at the end, when combining the string.

And for the otherway around, if I would have a string in the matchingArray that wouldn't be ending with a backslash, then the RegEx would break because it would end up like this \) causing Unterminated group.

 

A possible idea that I have, is that all \ could be converted into /, but what if you can't do that?

I don't see an issue doing that however, because I would be able to convert the / back into \ at any time. It would be an issue if those mixed slashes need to be preserved, but this would probably an even more specific problem.

But are there other ways?

karl-police
  • 983
  • 8
  • 21

0 Answers0