Convert Form input from upper to lower cases [duplicate] - javascript

This question already has answers here:
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Converting php string to Title Case
(3 answers)
Closed 4 months ago.
I would need a Custom JavaScript for a WordPress form, converting upper cases to lower cases. I found some entries here in the community, but not the one I need.
What should be converted ?
For example I get a form entry 'PETER' and I want that the first digit 'P' stays in upper cases and the rest should be converted to lower cases. From 'PETER' to 'Peter'
Who can give a helping hand, please ? Thanks

You can get the sting first char with str[0], the other parts sliced with -1 argument.
const name = "PETER";
function PETERToPeter(str) {
return str[0].toUpperCase() + str.slice(1).toLowerCase();
}
const nameChanged = PETERToPeter(name);
console.log(nameChanged);

Related

How come the following code doesn't change the retrieved letters into upper case? [duplicate]

This question already has answers here:
The .replace() method does change the string in place [duplicate]
(3 answers)
Replace method doesn't work
(4 answers)
Closed 23 days ago.
The goal of this was to change the retrieved letters into upper case. I know this works if I stored lines 6 and 7 inside a variable then replace the letters inside the variable called string, but I wanted to know why this code doesn't work instead.
JavaScript strings are not mutable, meaning you can't change the value of a character at a certain index. You'll need to make a new string made of these values.
let string = 'lowercasestring'; // Creates a variable and stores a string.
let letterOne = string.indexOf('l'); // Should store as 0.
let letterTwo = string.indexOf('s'); // Should store as 7.
// mutiple lines for readability
string = string[letterOne].toUpperCase() +
string.slice(letterOne + 1, letterTwo) +
string[letterTwo].toUpperCase() +
string.slice(letterTwo + 1);
console.log(string);
Output:
"LowercaSestring"

Replace dot in a number [duplicate]

This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')

Javascript slice concatenation [duplicate]

This question already has answers here:
JavaScript slice method?
(6 answers)
Closed 4 years ago.
I need to bump a string down 1 index thus moving index 0 to the back.
For example, turn the string '12345' into '23451'. The code below works but I just don't understand why/how.
How does the return statement remember to add '345' back into string s? Shouldn't it be returning the concatenation '21'?
let s = "12345"
let rotate = (function (){
return s.slice(1) + s.slice(0,1);
})
console.log(rotate(s))
"Shouldn't it be returning the concatenation '21'?"
s.slice(1) does not return an element at index 1, but everything that starts from index 1. So in your case it will stand for 2345 and finally will result in 23451.
The slice takes two arguments. If there is only one argument present, it will return the string FROM the first index (missing everything before that). If there are two arguments present, it will return a string containing the characters between the two indices. For this example, it is the first character (from 0 to 1). Then it just adds these two parts together and returns it.
You can read more about the slice function on Mozilla Docs.

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

How to grab all numbers from a string in JavaScript? [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 2 years ago.
Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000 or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
var words = sentence.split(" ");
var numbers = words.filter(function(w) {
return w.match(/\d+/);
})

Categories

Resources