Hello I have a string variable
var str = "Air Quality – Indoor"
which I converted into
var str2 = "air-quality-indoor"
using .replace(/-/g, ”).replace(/\s+/g, ‘-‘).toLowerCase()
so how can I convert "air-quality-indoor" into "Air Quality – Indoor" again ?
so how can I convert "air-quality-indoor" into "Air Quality – Indoor" again ?
>Solution :
Titlecase function taken from titlecase below is a sample code.
let str = "Air Quality - Indoor";
str = transform(str);
console.log(str);
str = transform(str);
console.log(str);
function transform(str) {
if (str.includes(' ')) {
return str.replace(/-/g, '').replace(/\s+/g, '-').toLowerCase()
} else {
return titleCase(str.replace(/-/g, ' ')).replace(/\b(\w+)$/g, '- $1');
}
}
function titleCase(str) {
return str.toLowerCase().split(' ').map(function(word) {
return (word.charAt(0).toUpperCase() + word.slice(1));
}).join(' ');
}