Testing for uppercase, why is toUppercase() alphanumeric in javascript? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I isolate toUpperCase() to disregard numbers
Live code
var data = 'LOL';
var data2 = '1 2 3 4';
if(data2 === data2.toUpperCase()) {
document.write('hey');
}else {
document.write('nope');
}
both will write hey to the document!
Why in javascript is toUppercase() think that numbers are uppercase letters? What is the best way to test for uppercase but not for numbers also?

You can use a regex match to see if the string contains all uppercase letters:
var uppercaseletters = /^[A-Z]+$/;
if(data2.match(uppercaseletters)) {
document.write('hey');
} else {
document.write('nope');
}

Why? That's how it's defined by the ECMA standard:
Let [the result] be a String where each character of L is either the Unicode
[uppercase] equivalent of the corresponding character of S or the actual
corresponding character of S if no Unicode [uppercase] equivalent
exists.
(Emphasis mine.)

toUpperCase() is only designed to capitalize letters. If you were to 'capitalize' a number, it would be an entirely different character. So, if you wanted to capitalize "1, 2, 3" it would then become "!, #, #" which is completely different from "a, b, c" becoming "A, B, C"
Also, might I add that there is no such thing as an 'Uppercase Number'
The same concept applies for toLowerCase()

Related

First name only Regular Expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I'm trying to come up with a regular expression for a first name only that should not contain the following:
Symbols, numbers, unusual capitalization or punctuation.
Characters from multiple languages – meaning one cannot have the Latin/roman alphabet mixed. with Arabic script or Katakana Japanese script.
I tried this which I found on this platform '/^(?=.{1,50}$)[a-z]+(?:['_.\s][a-z]+)*$/i'
But it accepts DaVid, DaaaaaaaaaaVid when it should fail them.
About the capital, adding [A-Z] and removing the i (case insensitive option) makes it:
const regex = /^(?=.{1,50}$)[A-Z][a-z]+(?:['_.\s][a-z]+)*$/
const names = [
"DaVid", // Rejected
"DaaaaaaaaaaaVid", // Rejected
"David", // Accepted
"Ann", // Accepted
"Soooooooophie", // Accepted
]
names.forEach((name) => console.log(regex.exec(name) ? "Accepted" : "Rejected"))
But for letters repeating more than 2 times, I don't know how that would be possible in just one regular expression.
While this one detects it:
const regex = /([a-z])\1{2,}/
const names = [
"DaVid", // Ok
"DaaaaaaaaaaaVid", // Repeating char detected
"David", // Ok
"Ann", // Ok
"Soooooooophie", // Repeating char detected
]
names.forEach((name) => console.log(regex.exec(name) ? "Repeating char detected" : "Ok"))

Is there way to change a substring in js? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
I have a string like:
def definition():
I want to change word def (for example), every instance of word def but not the "def"s that are part of other words
like this
console.log("def definition():".specialReplace("def", "abc"));
and result should be
abc definition():
not
abc abcinition():
Use String#replace or String#replaceAll with a regular expression:
const specialReplace = (str) => str.replaceAll(/\bdef\b/g, 'abc')
console.log(specialReplace("def definition")) // abc definition
console.log(specialReplace("def definition def")) // abc definition abc
In the regular expression, \b is a boundary type assertion that matches any word boundary, such as between a letter and a space.
Note that the same sequence \b is also used inside character class regular expression positions ([\b]), to match the backspace character.

using regex in javascript to check if foreign characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to create a simple function that checks if there are foreign characters in my string. Essentially, this is what I'm trying to get:
var str_1 = "отправка.py" // should return true
var str_2 = "bla.py" // should return false
var str_3 = "bla23.py" // should return false
var str_4 = "bla23_.py" // should return false
I want to make sure that there aren't any foreign characters, while still making sure that I allow everything else (characters like "_", "-", ...).
I'm just trying to avoid the Russian, Mandarin, etc.. alphabets.
You are looking only for ASCII. So something like:
!/^[\x00-\x7F]*$/.test('отправка.py'); // => true
!/^[\x00-\x7F]*$/.test('bla.py'); // => false
Code
See regex in use here
[^ -~]
Alternatively: [^\x00-\x7F] (which seems to have already been posted by #BlakeSimpson)
Usage
const r = /[^ -~]/
const a = [
"отправка.py",
"bla.py",
"bla23.py",
"bla23_.py"
]
a.forEach(function(s) {
if(r.exec(s) !== null) {
console.log(s)
}
})
Explanation
[^ -~] Matches everything that's not from the space character (DEC 32) to the tilde ~ symbol (DEC 126) - which are all the visible ASCII characters.
Also, note that I don't use the g modifier. This is intentional as the OP is only asking to check whether or not there are foreign characters in the string. That means that so long as 1 character exists in the string that meet those requirements it should be matched (no need to match more than one).

Regex Only one special character at a time from allowed special characters Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to allow 3 characters 1st Underscore (_) , 2nd hyphen (-) 3rd Dot (.)
but i want to put conditions that only one character is allowed at a time from all of them, and also only one time.
e.g
allowed usernames = abc.def , abc-def , abc_def
not allowed usernames = abc.de.f alert here(only one special character is allowed in a username)
not allowed usernames = abc.de-f , abc.de_f , ab-cd_ef
What should i do.
Try /^[a-z]*[-._]?[a-z]*$/
var tests = ['abc.def', 'abc-def', 'abc_def', 'abc.de.f','abc.de-f' , 'abc.de_f', 'ab-cd_ef'];
$.each(tests, function(a,b) {
$('body').append('<div>' + b + ' = ' + regIt(b) + '</div>');
});
function regIt(str) {
return /^[a-z]*[-._]?[a-z]*$/.test(str);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
^[a-z]*([-._]?)(?:[a-z]|\1)*$
This regex will match letters until it reaches the end of the string. If it reaches a symbol (- . or _) it will store that symbol as group 1, and keep matching for letters or that same symbol until the end of the string.
The following are matching examples:
an empty string
_something
something-something
foo_bar_baz
foo.
And here are some invalid strings:
my_file.txt
alpha.bravo_charlie
not-working_

Regex to evaluate a list of 6 digit numbers separated by commas [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have an input field that I would like users to put in a list of numbers that are 6 digits long. the list the users input will have variable lengths.
Pass
123456, 123457, 156545, 546541, 546541
Pass
123456, 123457
Pass
546541
Fail
12345, 155154
Fail
154s54, 159475, 153456
Fail
154s544, 159475, 153456
The regEx you are looking for is /^\d{6}$/, which matches a 6 digit number and only 6 digit number.
var cases = [
'123456, 123457, 156545, 546541, 546541',
'123456, 123457',
'546541',
'12345, 155154',
'154s54, 159475',
'154s544, 159475, 153456'
];
//Break up numbers in string into array the check each token
//against the regex. If all tokens passes the test, then it
//returns true, else false.
t = cases.map(c => c.split(', ')
.reduce((p, n) => p && !!n.match(/^\d{6}$/), true));
for (let i=0; i < cases.length;i++)
console.log('case:', cases[i], t[i]?'pass':'fail');
Assuming that you are not looking to capture the individual numbers, but just want to validate the input, the following regex should do:
^(\d{6},\s*)*\d{6}$
Breakdown of the regex:
^ beginning of the string
(\d{6},\s*)* zero or more occurrences of a 6-digit number, followed by a comma and optional whitespace
\d{6} a 6-digit number (this is the last and possibly the only one)
$ end of the string
Note that the expression enclosed within parentheses is a capture group. To avoid capture and make it stricter the expression would be written as:
^(?:\d{6},\s*)*\d{6}$
Note the ?: after the first parenthesis. It means match the expression but do not capture it.

Categories