arctic
arctic

Reputation: 819

Regex: remove everything except the letters and separator

I am currently using replace statements to replace certain parts of a string. I think my code is a bit over the top and could be simplified:

const locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5'
locales = locales.replace('-','_')
locales = locales.replace(';q=','')
locales = locales.replace(/[0-9]/g,'')
locales = locales.replace('.','')

In the end, I want to remove everything except for the locale from the string using regex and replace - with _. I would like the final string to look like this:

'fr_CH, fr, en, de, *'

Upvotes: 0

Views: 390

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386816

You could replace and search for two small characters and possible following dash and some more upper case characters or a star.

const
    locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5',
    parts = locales
        .replace(/-/g, '_')
        .match(/[a-z]{2}_*[A-Z]*|\*/g)
        .join(', ');

console.log(parts);

An even shorter approach which relaces all parts after semicolon (inclusive) until following comma without converting/matching to an array an joining back to a string.

const
    locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5',
    parts = locales
        .replace(/-/g, '_')
        .replace(/;[^,]+/g, '')

console.log(parts);

Upvotes: 1

A carefully chosen regular expression can strip out the weightings in one replacement. A second switches the hyphens - for underscores _

const locales = 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5';
newLocales = locales.replace(/;q=\d*\.\d*/g,'').replace(/-/g,'_');
console.log(newLocales);   //  fr_CH, fr, en, de, *

Upvotes: 1

Related Questions