2

I want to change my text in lowerCase but keep the first letter of each sentense uppercase.

Input

LOREM Ipsum is simply DUMMY text of the printing and typesetting industry. Lorem IPSUM. Has been the industry's standard dummy text ever since the 1500s, WHEN an unknown printer took a galley.

Expected

Lorem ipsum is simply dummy text of the printing and typesetting industry. Lorem ipsum. Has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley.

I am new to regexp, I tried this :

expected = input.replace(/([A-Z])+/g, "\$&".toLowerCase())

6 Answers6

2

You can do it like this

What i have done is first changed complete string to lowercase. now by using the regex i am searching for either the first word of the string. or any character which is preceded by . and any number of space characters and converting match to uppercase.

Regex

^([a-z])|\.\s*([a-z])
  • ^([a-z]) - Matches first word of string.
  • | - Alternation same as logical OR.
  • \.\s*([a-z]) - Matches any . followed by any number of space character followed by alphabet.

let str = "LOREM Ipsum is simply DUMMY text of the printing and typesetting industry. Lorem IPSUM. Has been the industry's standard dummy text ever since the 1500s, WHEN an unknown printer took a galley."

let op = str.toLowerCase().replace(/^([a-z])|\.\s*([a-z])/g, (match)=>match.toUpperCase())
console.log(op)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

This is a looped solution not a lot of regex

var str = input.split(" "); 
var expected = "";
str.map(function(c){
  expected += c.toLowerCase().replace(/^[a-z]/, c[0].toUpperCase())+ " "
})
expected = expected.trim(); 
jidexl21
  • 609
  • 7
  • 22
0

you don't have to stress out with regex as we can simply achieve this using a style attribute.

document.getElementById("inputId").style.textTransform = "initial";

For this, you need to find the id that is bind to that variable in HTML. That is all needed.

  • 2
    While this (probably) works, nowhere does OP mention that they're working with a DOM where you can set the style to achieve this. – nbokmans Dec 17 '18 at 10:40
0

Loop through each sentence and try javascript like
_str.charAt(0).toUpperCase() + _str.substr(1).toLowerCase() where _str is a sentence. This will be helpful when you don't have too many sentences to work on.

Abhinaw Kaushik
  • 607
  • 6
  • 18
0

You can also do it in plain javascript by passing all in lowercase and add capitals letter by splitting your string.

input.toLowerCase().split('.').filter(x=> x.length > 0).map(x=> x[0].toUpperCase() ).join('.')
Thomas
  • 1,008
  • 3
  • 16
  • 34
0
yourString
  .toLowerCase()
  .split(" ")
  .map(word => {
    word.charAt(0).toUpperCase() + word.substring(1, yourString.length
   // you can get rid of the curly brackets they are there to just make it readable
  }))
  .join(" ")
  • Turn the string to lower case
  • split words and get an array of words
  • use map to turn first character to uppercase and join it with the rest of the word with + operator (substring is there to get rid of the first letter)
  • join all the words by using .join()