bold specific string that is enclosed inside squeare brackets using javascript

81 Views Asked by At

Hi Javascript developers i am facing an issue about bold the string that is enclosed inside of square brackets. I have a string that is dynamically generated and the string look like

[Number] years of practice in accounting and financial administration. Showcased skills in [Area of Expertise]

I want to bold every letter inside a string that is enclosed inside the square bracket just like the above string ( [Number], [Area of Expertise] ).

Like Number and Area of Expertise must be bold. Remember that the string is dynamic.

1

There are 1 best solutions below

0
Anton Podolsky On

Please see below snippet.

Also, it's a good idea to html-escape the string before doing the replacement, and generally before inserting dynamic content into the DOM (read more).

const s = '[Number] years of practice in accounting and financial administration. Showcased skills in [Area of Expertise]';

const html = s.replace(/\[([^\]]+)\]+/g, '<b>$1</b>');

console.log(html);

// => <b>Number</b> years of practice in accounting and financial administration. Showcased skills in <b>Area of Expertise</b>