Convert text to lower case in JavaScript [duplicate] - javascript

This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 4 years ago.
I have this code which converts some text to lower case but is it possible to convert to 1st character to a capital letter followed by the remaining characters being lower case?
NWF$(document).ready(function() {
NWF$('#' + varTitle).change(function() {
this.value = this.value.toLowerCase();
});
});
Thanks to Ulysse BN , I got the hint to use the following
this.value = this.value[0].toUpperCase() + this.value.slice(1).toLowerCase()
which works excellent but if you wanted to use VGA as capital then this would not be possible, it is a catch 22 situation. Just wanted to avoid users from typing everything in capital (I hate it).

You could lowercase the whole string except the first letter:
let value = 'HEYhoHAHA'
value = value[0].toUpperCase() + value.slice(1).toLowerCase()
document.write(value)

Related

How to check if new line with character is present in string using javascript? [duplicate]

This question already has answers here:
Regex string ends with not working in Javascript
(11 answers)
Closed 1 year ago.
what is way to find if string has new line character(/n) followed by any other character in javascript?
Currently I'm doing this:
if (((mystring).slice(-2)) == "/\n/A") {
//mycode
}
I want to check whether last two characters of string are \n and A.
My thing is not working. Is there any other way to do this?
I want to check whether last two characters of string are \n and A.
You can use String#endsWith
const A = `this is a valid string\nA`;
const B = `this is an invalid string`;
console.log('A', A.endsWith('\nA'));
console.log('B', B.endsWith('\nA'));

Remove empty characters, blank characters, invisible characters in jQuery [duplicate]

This question already has answers here:
Remove not alphanumeric characters from string
(10 answers)
Regular expression to remove anything but alphabets and '[single quote]
(1 answer)
javascript regex to return letters only
(6 answers)
Ignoring invisible characters in RegEx
(2 answers)
Closed 2 years ago.
I am performing a validation in html text box which should pass only alphabets(a-z/A-Z) and few special characters like (*,& etc..). Otherwise it should show error some error.
I had written a JavaScript function which does the same.
function removeInvalidCharacters(selectedElement) {
if (selectedElement && typeof selectedElement.val() !== typeof undefined && selectedElement.val() != "") {
selectedElement.val(selectedElement.val().replace(/[\u0000-\u001F]|[\u007F-\u00A0]/g, "").replace(/\\f/g, "").replace(/%/g,""));
}
}
I am filtering selectedElement before passing to the function removeInvalidCharacters.
$("#name").val(toASCII($("#name").val()));
var selectedElement = $("#name").val();
But now I am facing a scenario in which empty characters, blank characters, invisible characters and whitespace characters are bypassing my regex. I could see some invisible characters are present in my name field. I want to replace these characters.
In further investigation I could found that Invisible characters - ASCII
characters mentioned in this link are the culprits. I need to have a regex to catch them and replace them.
Eg: AAAAAAAAAAAA‎AAAAAAAAAAA is the value in text field. Now if we check $("#name").val().length, it gives 24 ,even though we could see only 23 characters. I need to remove that hidden character.
Please help me with this scenario. Hope my query is clear
UPDATE:
var result = selectedElement.replace(/[\u200B-\u200D\uFEFF]/g, ''); fixed my problem.
Thank you all for the support.
If you want to allow only (a-z/A-Z) like you mention, try this:
str = str.replace(/[^a-zA-Z]/g, '');
Include the chars you want to keep instead of the ones you do not want, since that list may be incomplete
Otherwise look here: Remove zero-width space characters from a JavaScript string
const val = `AAAAAAAAAAAA‎AAAA**AAAAAAA`;
const cleaned = val.replace(/[^A-Za-z*]/g,"");
console.log(val.length,cleaned.length);

JavaScript how to strip/remove a char from string [duplicate]

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'

How do I properly use RegExp in JavaScript and PHP? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How do I properly use RegExp?
var text = "here come dat boi o shit waddup";
var exmaple = /[a-zA-Z0-9 ]/; // allowes a-zA-Z0-9 and whitespaces but nothing else right?
example.test(test); // would return true right?
text = "%coconut$§=";
example.test(text); // would return false right?
//I know this is very basic - I started learnig all this about week ago
Are JS RegExp's the same as PHP RegExp's?
How do I define banned characters instead of defining allowed characters?
How do I make it so that the var text has to contain 3 (or more) numbers/letters?
How do I include / or ",'$ etc. in my pattern?
No.
Use ^ character (i.e. [^abc] will exclude a, b and c)
Use [A-Za-z]{3} for letters and \d{3} for digits. If you want 3 or more, use \d{3,}
Use escape character (\/, \', \", '\$')

How to downcase all char and uppercase only the first characters via js? [duplicate]

This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 8 years ago.
Now I have this regex:
tSzoveg = tSzoveg.replace(/\b[A-Z]{2,}\b/,'');
It is almost that I want. I want to convert this: THIS IS MINE, NOT YOURS. to this: This is mine, not yours.
How can I convert it to a normal sentence?
function capitalize(string)
{
return string.toUpperCase().charAt(0) + string.toLowerCase().slice(1);
}
I would use toLowerCase() and then modify the first letter:
var str = "THIS IS MINE, NOT YOURS."
str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();

Categories

Resources