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

string space convert into – and after that convert into same string how?

Hello I have a string variable

var str = "Air Quality – Indoor"

which I converted into

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

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(' ');
}
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