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"))
Related
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.
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).
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.
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 7 years ago.
Improve this question
I have the below wild card search which looks for the following conditions.
$.validator.addMethod("FirstName", function (value, element) {
return this.optional(element) || /^[a-zA-Z''-'\s]{3,20}[\%\*]{0,1}$/i.test(value);
}, "For wild card search minimum of 3 characters should be entered.");
$("#frmJscsSearch").validate({
rules : {
FirstName : "required FirstName"
},
});
if user enter 2 characters, its valid
If user enter 3 characters, its valid
How do I validate if user enter 2 characters WITH * then its invalid?
See if this regex does what you want (the idea is to use alternation |):
/(^[a-z '\-]+$)|(^[a-z '-]{3,}[\%\*]$)/i
Your original regex was a little messy also, so I've edited it.
You can also try checking for the wrong input instead of the right one:
var re = /([^a-z '\-*%])|(^[\w\W]{0,2}[\*\%]&)|(^[ \-*%'])|([ \-]$)|([ \-']{2,})/i
!(re.test(value))
That way you can check for more specific conditions easier. The regexp above describes the following rules:
value should contain only a-z '-*% characters;
* or % should be the last symbol (if used) preceded by at least three other characters;
value can't start with -'*%;
value can't end with -;
value can't contain multiple -' in a row.
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()