1

I'm using replace to find special codes with regular expression and replace them with the substring modified.

// More stuff happens inside the function, but this was just simplified
x.replace(/start([a-f0-9]{16})end/g, function(code){return code;});

The codes always start and end the same way and they're pretty easy to match with regular expression. However, I'd like to only pass what's in the parentheses to the function. Is that possible?

GFL
  • 1,278
  • 2
  • 15
  • 24
  • In modern environments, [use lookbehind and lookahead instead](https://stackoverflow.com/a/57450029) – CertainPerformance Mar 13 '21 at 02:43
  • @CertainPerformance, that would seem to work if I just wanted to isolate and return certain parts, but I need to pass it to the function for more complex modification. How would I pass it to a function? – GFL Mar 13 '21 at 02:51
  • By using lookbehind and lookahead in your pattern instead of a capturing group. See the answer I linked for an example. – CertainPerformance Mar 13 '21 at 03:00
  • Usually, you'll use $1 as the second argument, to capture the first group, $2 for the second group, $N Numbered groups thereafter. – bob Mar 13 '21 at 03:09

0 Answers0