maimok
maimok

Reputation: 373

Javascript - Adjusting string to title case logic

I am attempting to convert my string into title case which I'm able to do successfully. using this method:

title.toLowerCase().split(' ').map(function (s) {
        return s.charAt(0).toUpperCase() + s.substring(1);
    }).join(' ');

But this current logic applies well for one specific title schema. I am attempting to customize it to work better in different scenarios but when doing so none of them have an effect on the outcome of the title.

For starters as seen in the code snippet below, the word (case) is lower case and that should be capitalized too. It seems to not capitalize the first letter after parentheses.

I also attempt to have an outcome where Tv = TV replace the word and = &. I am approaching this using the .replace method

How can I adjust the code snippet below in order to add these conditions?

let title = "Adjusting the title (case and test) on tv"

titleCase = title.toLowerCase().split(' ').map(function (s) {
        return s.charAt(0).toUpperCase() + s.substring(1);
    }).join(' ');
    
titleCase.replace(/ and /ig, ' & ');

titleCase.replace("Tv", "TV")

console.log(titleCase)

My expected outcome is : Adjusting The Title (Case & Test) On TV

Upvotes: 1

Views: 4303

Answers (5)

tonitone120
tonitone120

Reputation: 2290

letterToCapitalize finds the first occurrence of [a-zA-Z0-9_] in each word you're trying to capitalize (the first suitable letter of).

In your code, you must remember that .replace(...) actually returns a brand new string. It does not modify the string in place (in JavaScript, primitive data-types, of which strings are one, are not modifiable). The problem was you weren't assigning this new string to a variable that you were logging out.

let title = "Adjusting the title (case and test) on tv"

let titleCase = title.toLowerCase().split(' ').map((s) => {
    let letterToCapitalize = s.match(/\w/)[0];
    return s.replace(letterToCapitalize, letterToCapitalize.toUpperCase())
    }).join(' ');
    
titleCase = titleCase.replace(/and/ig, '&').replace("Tv", "TV");

console.log(titleCase)

Upvotes: 5

Hussein AbdElaziz
Hussein AbdElaziz

Reputation: 500

the solution in 1 line using Regex

const str = 'Adjusting the title (case and test) on tv';
const titleCase = (str) => str.replace(/\b[a-z]/gi, (cahr) => cahr.toUpperCase()).replace(/Tv/gi, 'TV');

Upvotes: 0

AMD
AMD

Reputation: 203

With the below method, all you need to do is to specify the words that should be converted to symbols in the symbol object and the words that need to be all upper case in the allUpperCase array.

let title = "Adjusting the title (case and test) on tv";
let reg  = /\w+/g;

// convert all words to titleCase
title = title.replace(reg, (word) => {
  return word.charAt(0).toUpperCase() + word.slice(1);
});

// convert words that should be symbols
const symbols = {'and': '&'};
for(symbol of Object.keys(symbols)){
  let str = symbol;
  title = title.replace(new RegExp(str, 'ig'), symbols[symbol]);
}

// convert all words that need to be allUppercase
const allUpperCase = ['TV'];
allUpperCase.forEach(item => {
  title = title.replace(new RegExp(item, 'ig'), found => {
    return found.toUpperCase();
  });
});

console.log(title);

Upvotes: 0

Emy
Emy

Reputation: 639

this is the solution:

let title = "Adjusting the title (case and test) on tv";
let titleCase;

titleCase = title
    .toLowerCase()
    .split(" ")
    .map(function (s) {
        return s.charAt(0) == '(' ? '(' + s.charAt(1).toUpperCase() + s.substring(2) : s.charAt(0).toUpperCase() + s.substring(1);
    })
    .join(" ")
    .replace(/ and /gi, " & ")
    .replace(/Tv/gi, "TV");

console.log(titleCase);

Upvotes: -1

Emy
Emy

Reputation: 639

solution is:

let title = "Adjusting the title (case and test) on tv";
let titleCase;

titleCase = title.toLowerCase().split(' ').map(function (s) {
        return s.charAt(0).toUpperCase() + s.substring(1);
    }).join(' ').replace(/ and /ig, ' & ').replace(/Tv/ig, "TV");

console.log(titleCase);

Upvotes: 0

Related Questions