Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Substitute all letters except the first one in all words

var substitute_with = "_";
const regex = /\B\w/g;

var result = text.replaceAll(regex, substitute_with);

This code substitutes all letters with underscore, except the first one in all words.

This works fine for the English language, but I need the same with both English and Cyrillic.

Example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Это текст-"рыба", часто используемый в печати и вэб-дизайне.

Online regex: https://regex101.com/r/scjHae/1

Could you help me?

>Solution :

You could use Unicode property escapes (with u flag), and instead of \B, use a look behind assertion:

const result = "They all shouted Слава Україні!".replaceAll(/(?<=\p{L})\p{L}/gu, "_");
console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading