How to add point classes?

There is a line: const str = 'class1 class2';
I tried this: const newstr = str.replace(/^[^\d]/g, .$1);

It's like this: '.class1 .class2'
April 7th 20 at 15:46
2 answers
April 7th 20 at 15:48
Solution
const newstr = str.split(" ").map(cls => '.${cls}').join(" ");


'.${cls}' here will replace the brackets for horizontal interpolation
Using regular expressions will not work ? - Cullen.Emmerich commented on April 7th 20 at 15:51
@Donnie_Crist77, it will, just what this method is not satisfied? - Maryjane commented on April 7th 20 at 15:54
@Junior.Ryan8, my mistake, you need with the help of regular expressions - Cullen.Emmerich commented on April 7th 20 at 15:57
@Donnie_Crist77, the regular season will tell. - Maryjane commented on April 7th 20 at 16:00
@Junior.Ryan8, thank you - Cullen.Emmerich commented on April 7th 20 at 16:03
April 7th 20 at 15:50
Solution
str.replace(/(?:^|\s+)(\w)/g, ' .$1');

Find more questions by tags JavaScript