You can use this regex expression: /(<\/?h)([0-6])/
with global and ignore case flags.
Answering your comments:
It's just replacing the string with the new value. With replace method you can use $n
as a parameter. In my example I added two groups, if we have a match, i concatenate the first match parameter (</?>) with "2".
You can read more about "replace" here (Section: "Specifying a string as a parameter"):
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
The array + map method is just to test/exemplify more than 1 option.
You can also use without RegExp.
yourString.replace(/(<\/?h)([0-6])/ig, "$12"); //same result
Sample:
const reg = new RegExp(/(<\/?h)([0-6])/, 'ig');
const values = [
'<h1>Hello world</h1>',
'<h2 class="something">Hello world</h2>',
'<h3 id="a">Hello world</h3>',
'<h2>Hello world</h2>'
]
const newValues = values.map(item => item.replace(reg, "$12"))
console.log(newValues);