function escapeDelimiters(pattern, text) {
  return text.replace(pattern, (match, start, end) => {
    const hasEnd = typeof end === 'string';
    const matchSliceEnd = hasEnd ? match.length - end.length : match.length;
    const content = match.slice(start.length, matchSliceEnd);
    return `${escape(start)}${content}${hasEnd ? escape(end) : ''}`;
  });
}
function escapeDelimiters(pattern, text) {
  return text.replace(pattern, (match, start, end) => {
    const hasEnd = typeof end === 'string';
    const matchSliceEnd = hasEnd ? match.length - end.length : match.length;
    const content = match.slice(start.length, matchSliceEnd);
    return `${escape(start)}${content}${hasEnd ? escape(end) : ''}`;
  });
}
function escapeDelimiters ( pattern , text ) { return text . replace ( pattern , ( match , start , end ) => { const hasEnd = typeof end === 'string' ; const matchSliceEnd = hasEnd ? match . length - end . length : match . length ; const content = match . slice ( start . length , matchSliceEnd ) ; return ` ${ escape ( start ) } ${ content } ${ hasEnd ? escape ( end ) : '' } ` ; } ) ; }
escapeDelimiters

Executes `String.replace` for a given pattern, but only on the first two
capture groups. Specifically intended for escaping opening (and optionally
closing) markdown entities without escaping the content in between.
escapeDelimiters
Escape delimiters in text