Use regex to replace lower case values to title Case JavaScript [duplicate] - javascript

This question already has answers here:
Convert string to Title Case with JavaScript
(68 answers)
Closed 4 years ago.
I want to use regex to replace the lowercase letter of every word within a string of words, with an uppercase, ie change it to title case such that str_val="This is the best sauce ever"
becomes "This Is The Best Sauce Ever".
Here is my code
function (str_val) {
let reg_exp = /\s\w/g;
let str_len = str_val.split(' ').length;
while (str_len){
str_val[x].replace(reg_exp, reg_exp.toUpperCase());
x++;
str_len--;
}
return str_val;
}
How do I solve this with regex?

Use below function for title case
function title(str) {
return str.replace(/(?:^|\s)\w/g, function(match) {
return match.toUpperCase();
});
}

Related

I need a comprehensive regex to create a hyphened word chaining [duplicate]

This question already has answers here:
Regular rxpression to convert a camel case string into kebab case
(4 answers)
Closed 2 years ago.
The aim of the challenge is to create a hyphened word chaining. My question is how I could create an all-encompassing regex for the scenarios shown below:
I am able to do the first, third and fourth example.
function spinalCase(str) {
let result =str.toLowerCase().split(/\W/)
result.join('-')
return result;
}
spinalCase('This Is Spinal Tap');
spinalCase("Teletubbies say Eh-oh");
spinalCase("The_Andy_Griffith_Show");
spinalCase("thisIsSpinalTap")// My code does not work on these
spinalCase("AllThe-small Things")// My code does not work on these
You can use the following regular expression:
Reference: https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-123.php
var spinalCase = function(str) {
var converted = str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('-');
console.log(converted)
return converted;
};
spinalCase('This Is Spinal Tap');
spinalCase("Teletubbies say Eh-oh");
spinalCase("The_Andy_Griffith_Show");
spinalCase("thisIsSpinalTap")
spinalCase("AllThe-small Things")

Capitalizing certain strings in an array [duplicate]

This question already has answers here:
Convert string to Title Case with JavaScript
(68 answers)
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Closed 4 years ago.
I basically want to capitalize the first letter in every word in a sentence, assuming that str is all lowercase. So here, I tried to split the string, letter by letter, then by using for loop, I would capitalize whatever the letter that's after a space. Here's my code and could you please point out where I coded wrong? Thank you.
function titleCase(str) {
var strArray = str.split('');
strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You need to assign the values:
function titleCase(str) {
var strArray = str.split('');
strArray[0] = strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1] = strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You can try following
function titleCase(str) {
var strArray = str.split(' ');
for (i=0; i<strArray.length;i++){
strArray[i] = strArray[i].charAt(0).toUpperCase() + strArray[i].slice(1);
}
return strArray.join(' ');
}
console.log(titleCase("i am a sentence"));

How do I lowercase any string and then capitalize only the first letter of the word with JavaScript? [duplicate]

This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 6 years ago.
I'm not sure if I did this right, as I am pretty new to JavaScript.
But I want to lowercase any random string text and then capitalize the first letter of each word in that text.
<script>
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function lowerCase(string) {
return string.toLowerCase();
}
</script>
Just change the method to
function capitalizeFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
.toLowerCase() is appended to the last method call.
This method will make the first character uppercase and convert rest of the string to lowercase. You won't need the second method.
A small sample:
function firstLetter(s) {
return s.replace(/^.{1}/g, s[0].toUpperCase());
}
firstLetter('hello'); // Hello

How to check regex true on no repeating letters? [duplicate]

This question already has answers here:
Check for repeated characters in a string Javascript
(14 answers)
Closed 7 years ago.
I need to check if a word is Isogram, the meaning of Isogram, is that a word has no repeating letters.
I need to implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
Here is a test case
isIsogram( "Dermatoglyphics" ) == true
isIsogram( "aba" ) == false
isIsogram( "moOse" ) == false // -- ignore letter case
I am thinking on do this with a Regex.
function isIsogram(str){
//...
}
can you help?
as simple as that
function isIsogram (str) {
return !/(\w).*\1/i.test(str);
}
This will do:
function isIsogram (str) {
return !/(.).*\1/.test(str);
}
You can use it like this by converting the input to a lower case:
var re = /^(?:([a-z])(?!.*\1))*$/;
function isIsogram(str) {
return re.test( str.toLowerCase() );
}
Testing:
isIsogram("Dermatoglyphics")
true
re.test("aba")
false
isIsogram("moOse")
false

JavaScript space-separated string to camelCase [duplicate]

This question already has answers here:
Converting any string into camel case
(44 answers)
Closed 8 years ago.
I've seen plenty of easy ways to convert camelCaseNames to camel Case Names, etc. but none on how to convert Sentence case names to sentenceCaseNames. Is there any easy way to do this in JS?
This should do the trick :
function toCamelCase(sentenceCase) {
var out = "";
sentenceCase.split(" ").forEach(function (el, idx) {
var add = el.toLowerCase();
out += (idx === 0 ? add : add[0].toUpperCase() + add.slice(1));
});
return out;
}
Explanation:
sentenceCase.split(" ") creates and array out of the sentence eg. ["Sentence", "case", "names"]
forEach loops through each variable in the array
inside the loop each string is lowercased, then the first letter is uppercased(apart for the first string) and the new string is appended to the out variable which is what the function will eventually return as the result.

Categories

Resources