How can I convert spaces to "-" in a string and then convert it back to the original string?
P粉936509635
P粉936509635 2023-09-11 18:14:37
0
1
597

Hello, I have a string variable

var str = "Air Quality - Indoor"

I convert it to

var str2 = str.replace(/-/g, '').replace(/\s+/g, '-').toLowerCase()
//air-quality-indoor

So how to convert "air-quality-indoor" into "Air Quality - Indoor" again?

P粉936509635
P粉936509635

reply all(1)
P粉447002127

Titlecase function obtained 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, '- ');
  }
}

function titleCase(str) {
  return str.toLowerCase().split(' ').map(function(word) {
    return (word.charAt(0).toUpperCase() + word.slice(1));
  }).join(' ');
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template