Reputation: 763
My requirement is not only capitalize first character of string, there is a paragraph and I want capitalize first character of each string like mobile device keyboard.
I've tried below solutions but that doesn't fulfill my requirement:
$('#test').blur(function(event) {
var value = $(this).val();
var output = "";
output = value.charAt(0).toUpperCase() + value.slice(1);
});
above code only doing first character capitalize.
Sample 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 of type and scrambled it to make a type specimen book.that's it!
Expected output:
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 of type and scrambled it to make a type specimen book.That's it!
Upvotes: 1
Views: 99
Reputation: 1413
try this
var 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 of type and scrambled it to make a type specimen book.That's it!";
console.log( input.charAt(0).toUpperCase() + input.slice(1))
Upvotes: 1
Reputation: 7891
let paragraph = document.querySelector('#test').textContent;
let result = paragraph.split('.')
.map(sentence => sentence.trim())
.map(sentence => {
sentence = sentence.replace(sentence[0], sentence[0].toUpperCase()) + sentence.slice(1);
return sentence;
});
console.log(result.join('. '));
Upvotes: 0
Reputation: 370729
You can use a regular expression: match either the start of the string or a period followed by spaces, then match an alphabetical character, and use a replacer function to call toUpperCase
on that character. To also correctly replace letters that come after ?
and !
s as well as .
, use a character set [.?!]
:
const 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 of type and scrambled it to make a type specimen book.that's it!`;
const cleaned = input.replace(
/(^|\[.?!] *)([a-z])/g,
(_, g1, g2) => g1 + g2.toUpperCase()
);
console.log(cleaned);
To not replace ellipsis, you can match one more character before the .
in the first group:
const input = `foo bar. bar baz... baz buzz? buzz foo`;
const cleaned = input.replace(
/(^|(?:[?!]|[^.]\.) *)([a-z])/g,
(_, g1, g2) => g1 + g2.toUpperCase()
);
console.log(cleaned);
The pattern
(^|(?:[?!]|[^.]\.) *)([a-z])
means:
(^|(?:[?!]|[^.]\.) *)
- Capture in 1st group:
^
- Beginning of string, or:(?:[?!]|[^.]\.)
- Match the end of a previous sentence: either
[?!]
- Question or exclamation mark, or[^.]\.
- Non-period, followed by period*
Followed by any number of space characters([a-z])
- Capture in 2nd group any alphabetical characterThen
(_, g1, g2) => g1 + g2.toUpperCase()
replaces with the first group, concatenated with the second group lowercased.
Upvotes: 2
Reputation: 30360
You could match use the String#replace()
method with the following regular expression:
/\.\s*\w/
combined with a custom replace callback that capitalizes the matched string to achieve this. The basic idea here is to capitalize any substring that is the first letter directly following a full stop character (ie \.
). The regular expression also take into account cases where zero or more whitespaces (ie \s*
) occur between the full stop and the next letter character (ie \w
):
var 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 of type and scrambled it to make a type specimen book.That's it!";
var result = input.replace(/\.\s*\w/, function(match) {
return match.toUpperCase();
});
console.log('Regular expression based approach', result);
Also, I noticed in a comment that you asked about a method that did not require regular expressions. Although regular expressions are generally preferred from problems like this, the following shows a non-re based approach:
const 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 of type and scrambled it to make a type specimen book.that's it!`;
console.log('Non regular expression based approach:',
input
.split('.')
.map((sentence) => {
for (var i = 0; i < sentence.length; i++) {
if (sentence[i] !== ' ') {
sentence = sentence.substr(0, i) + sentence[i].toUpperCase() + sentence.substr(i + 1);
break;
}
}
return sentence
})
.join('.')
)
Upvotes: 0