There is a string like this
var string =
`<!-- paragraph {"className":"123"} -->
<p>abc</p>
<!-- /paragraph -->
<!-- paragraph {"className":"456"} -->
<p>cde</p>
<!-- /paragraph -->
<!-- paragraph {"className":"789"} -->
<p>fgh</p>
<!-- /paragraph -->`
const regex = /"className":"(.+)"/g;
string=string.replace(regex, '');
console.log(string);
I want to delete all characters except those following the className.
In other words, I want to make it look like 123456789 in the end.
If you can tell me a better way, I would appreciate your advice.
Any method other than replace is fine.
>Solution :
Use match then replace the string you don’t want
var string =
`<!-- paragraph {"className":"123"} -->
<p>abc</p>
<!-- /paragraph -->
<!-- paragraph {"className":"456"} -->
<p>cde</p>
<!-- /paragraph -->
<!-- paragraph {"className":"789"} -->
<p>fgh</p>
<!-- /paragraph -->`
const regex = /"className":"(.+)"/g;
let arr = string.match(regex, '');
arr = arr.map(e => e.replace(`"className":"`, '').slice(0, -1))
let str = arr.join('')
console.log(str);