Replace method in Javascript

Hi again :slight_smile:

hope all is well, and I’ll get straight into it!

I was looking at alternative solutions to a coding challenge I came across on codesignal and came across this solution:

function reverseInParentheses(inputString) {
    while (inputString.includes('(')) {
        inputString = inputString.replace(/\(([^()]*)\)/, (_, str) => [...str].reverse().join(''));
    }
    return inputString;
}

now, whilst I understand in natural language terms, I don’t understand why they have two arguments in the replacer function section, what does the underscore in (_, str) => [...str].reverse().join('')); represent?

Thanks for the help!

Don,

It’s what’s in the capture group
See here

2 Likes

The underscore is being used to signal that the first argument to the function should be ignored. You could have used an actual name instead, but since the first argument isn’t being used in the function body a lot of people will use the underscore as sort of a confirmation that the argument is not being used.

The str parameter is the string that’s inside the capture group (inside the parens in the regexp). If the string contains “(hello)” then when the regexp matches on it the str parameter will hold the string “hello”.

P.S. This code is not good, it has a potential infinite loop. My suggestion would be to get rid of the while loop completely and let replace do all the work. You can easily get this function down to a single return statement.

1 Like

Thank you, but I haven’t come across this way of pointing towards the capture group before, and I can’t see where it is referenced in the documentation?

Or is it generally a semantic rule in JS that func1(arg1, (_, arg2) => {...}) where _ refers to arg1?

does this apply to all data types or only some?

Thanks again for your help! :slight_smile:

Don

I think I follow your reasoning, but why would you need to do that? Why not just write it as it’s shown in documentation?

inputString.replace(/\(([^()]*)\)/, (str) => [...str].reverse().join(''));
    }

As for the reason behind the while, I understand it to be needed because the input is always guaranteed to be in a regular bracket sequence, but can have nested brackets that need to be processed sequentially.

the underscore is used to indicate that the first parameter will not be used

in the code you posted, matching it with the documentation, the underscore correspond to match and str to p1

here str equals the whole text matched, parenthesis included - instead the parenthesis seems that they should be removed

1 Like

Awesome, finally get it now :slight_smile: thank you!

In the original code you posted there is a chance for an infinite loop. The code assumes that the string will always have matching left/right parens, but what if it doesn’t? Perhaps there are extra checks on the string before it is passed to reverseInParentheses? But why even take that chance, especially when you can get rid of the while loop and let the regexp handle everything.

I am not saying that the regexp is wrong or that the code isn’t using the replace method properly. What I’m saying is that the while loop has a potential bug and it isn’t even needed in the first place. You can reduce this function to one line with a simple change to the regexp in the replace and at the same time get rid of this bug.

Actually, I just realized that my suggestion also has a bug because it doesn’t take into account nested parens, so I think you probably will need a while loop to make this work but it won’t be a simple test to see if the string still includes a (.