JSFiddle - React, Tailwind, and code Playground

by Artem

JavaScript

'use strict';

const str = '/paragraphs/headnotes';

const res = str
  .replace(/\//g, () => {
    return '\\/';
  })
	.replace(/\w+/g, (m) => {
		return m + '\\[\\d+\\]';
	});
	
	
	const re = new RegExp(res, 'g');

console.log(re, re.exec('/paragraphs[0]/headnotes[0]'));