numbers cannot follow characters - javascript

I'm trying to match a string that numbers cannot follow characters
For example, these should all match:
abc-123
123-abc
123-abc-123
abc-123-abc
not match:
abc123
123abc
abc1
a1b2
Please help me to find a javascript regex.

To match strings that contain only letters separated by numbers, you can use the following regular expression:
/^[a-zA-Z]+(?:-[a-zA-Z]+)*$/
This regular expression will match strings that start and end with letters, and have a number in between. The letters and the number must be separated by a hyphen.
For example, you can use the following code to test this regular expression:
const regex = /^[a-zA-Z]+(?:-[a-zA-Z]+)*$/;
console.log(regex.test("abc-123")); // should return true
console.log(regex.test("123-abc")); // should return true
console.log(regex.test("123-abc-123")); // should return true
console.log(regex.test("abc-123-abc")); // should return true
console.log(regex.test("abc123")); // should return false
console.log(regex.test("123abc")); // should return false
console.log(regex.test("abc1")); // should return false
console.log(regex.test("a1b2")); // should return false
This code defines a regular expression using the pattern described above, and then uses the test method to check if various strings match the pattern. The test method returns a boolean indicating whether the string matches the pattern or not.
Edited, since the question changed (I am still not sure if it is correct).

Related

Regular expression to exclude alphabetic characters

Can anyone suggest a correct regex pattern in angular 2 for accepting numbers and special charterers for angular 2 input field , excepting alphabets
This question has nothing to do with angular, but you can accomplish this with plain javascript. The regexp object's test method returns a boolean showing whether or not your string matches the regular expression.
/^[^a-z]+$/i.test("!##$%") == true // true
/^[^a-z]+$/i.test("Hello") == true // false
In this regex, the initial ^ is the beginning of line anchor and the trailing $ is end of line. The rest of the expression matches 1 or more characters that's not alphabetical. The i flag at the end makes it case-insensitive.
There are a number of nice online regex testing tools. One is https://regex101.com/.

Test if string is in Upper Camel Case format with only alphabetic characters

I try to test if the following rules apply on a string:
Must start with uppercase letter.
Next characters can be either in uppercase or lowercase format.
Only the following characters are allowed: A-z
For instance, a string can be either String or CamelCaseString, but neither string nor Camel-Case-String nor String125. Numerics and special characters must not exist.
I found this answer in a previous post.
const isUpperCamelCase = (str) => {
return /\b[A-Z][a-z]*([A-Z][a-z]*)*\b/.test(str)
}
I have the following test suit that tries to test the above function. Unfortunately, not all tests pass:
test('isUpperCamelCase', () => {
expect(isUpperCamelCase('Button')).toBeTruthy()
expect(isUpperCamelCase('GreenButton')).toBeTruthy()
expect(isUpperCamelCase('GreenLargeButton')).toBeTruthy()
expect(isUpperCamelCase('B')).toBeTruthy()
expect(isUpperCamelCase('button')).toBeFalsy()
expect(isUpperCamelCase('buttonCamel')).toBeFalsy()
expect(isUpperCamelCase('Green-Button')).toBeFalsy() // => fail!
expect(isUpperCamelCase('Button125')).toBeFalsy()
expect(isUpperCamelCase('Green_Button')).toBeFalsy()
expect(helpers.isUpperCamelCase('Green+Button')).toBeFalsy() // => fail!
expect(helpers.isUpperCamelCase('green+Button')).toBeFalsy() // => fail!
})
If I include special characters such as (,)+- in my string the function evaluates to true when it should evaluate to false. this happens because there exists a match between the special chars, but this is not the behavior that I want. How can I solve this problem?
Note: Please add detail explanation in your answer. Thanks! :)
You regex needs anchors for matching beginning and ending of the string, not \b. So do:
/^[A-Z][A-Za-z]*$/

Matching words as separate strings unless they start with a capital letter

I have this regexp
/[A-Za-zÀ-ÿ]+/g
that matches 'words' composed by characters of unlimited lenght.
If I do want to exclude words starting with a capital letter?
I tried
/(^[A-Z])[A-Za-zÀ-ÿ]+/g
but it doesn't seems to work. Can't use things like /w for it doesn't include diacritics.
EDIT: the language in use is Typescript so the javascript engine (which doesn't allow lookbehind, for example) Sorry for not mention this.
EDIT: the input given can be something like
"foo" //should match foo and return true
"Foo" //should not match foo and return false
"fòo" //should match fòo and return true
" " //should not match foo and return false
"." //should not match foo and return false
"," //should not match foo and return false
Code (Typescript) matching without the capital letter thing
isProperWord(word){
/* rejects
- string that are not words (symbols, spaces, etc...)
- names (words starting with a capital letter)
*/
if(word.match(/[A-Za-zÀ-ÿ]+/g)){
return true;
}else{
return false;
}
}
The expression ^[A-Z] means match an uppercase character at the beginning of line. You probably tried to type [^A-Z] which matches a character which is not an uppercase alphabetic between A and Z, but that still doesn't help, because the regex engine will find a character somewhere which matches this, and be satisified. (For example, a space trivially matches this -- it's a character, and it's not in the range A through Z.)
If you use a regex dialect which understands word boundaries with \b, try
/\b[a-z][A-Za-z]*/
to match a token which has a word boundary on its left, and a lowercase character adjacent to it. (I am ignoring your locale extension, which is not portable and possibly not well-defined.)
In isolation, the /g flag doesn't do anything. If you have a language which supports it, and use a regex in a while loop or similar, it will cause the engine to return all the matches in the string, one at a time, inside the loop; but without further context, we have no idea whether that is actually true here.
To match all capital letters from your initial range, you may use [A-ZÀ-ÖØ-Þ] character class. To match all lowercase letters, [a-zß-öø-ÿ]. Note that × and ÷ are not letters, I removed them from these classes.
To make sure the whole string consists of these letters only, and the first char is not an uppercase letter, use
/^[a-zß-öø-ÿ][A-Za-zÀ-ÖØ-öø-ÿ]*$/
See the regex demo.
JS demo:
var strs = ['foo','fòo','Foo',' ','.',','];
var rx = /^[a-zß-öø-ÿ][A-Za-zÀ-ÖØ-öø-ÿ]*$/;
for (var s of strs) {
console.log(s,"=>",rx.test(s));
}
To extract words, use custom boundaries:
var s = 'foo,fòo,Foo';
var rx = /(?:[^A-Za-zÀ-ÖØ-öø-ÿ]|^)([a-zß-öø-ÿ][A-Za-zÀ-ÖØ-öø-ÿ]*)(?![A-Za-zÀ-ÖØ-öø-ÿ])/g;
var m, res=[];
while(m=rx.exec(s)) {
res.push(m[1]);
}
console.log(res);

how to write a regular expression that ONLY accepts strings [duplicate]

This question already has answers here:
Regular Expression to match only alphabetic characters
(5 answers)
Closed 7 years ago.
I am trying to understand regular expressions, i find it really confusing to understand it. I need to know how to write a regular expression that ONLY matches strings, NO NUMBERS, NO SPECIAL CHARACTERS, just letters from A - Z. I have to say that I tried using \[a-z]\ but it didn't work as i wanted. These are some of the results that I want:
pe<.?ter -----> should return false
ale8 ---------> should return false
?ramon -------> should return false
tho<>?mass----> should return false
peter -----> should return true
alex ------> should return true
ramon -----> should return true
thomas ----> should return true
This should work for you:
var re = /^[a-z]+$/i;
See here
Your regex returns true if it find ONE letter from a to z, only lowercase, in your string.
To make what you want, use /^[a-zA-Z]+$/ or /^[a-z]+$/i.
^ make that your regex start from the begining of the string.
[a-zA-Z] make the regex to find one letter, lowercase OR uppercase (note that you could use instead the i modifier which make the search case insensitive).
+ make the regex to search for one or more times the previous element.
$ make sure that your regex end at the end of the string.

Matching accented characters with Javascript regexes

Here's a fun snippet I ran into today:
/\ba/.test("a") --> true
/\bà/.test("à") --> false
However,
/à/.test("à") --> true
Firstly, wtf?
Secondly, if I want to match an accented character at the start of a word, how can I do that? (I'd really like to avoid using over-the-top selectors like /(?:^|\s|'|\(\) ....)
This worked for me:
/^[a-z\u00E0-\u00FC]+$/i
With help from here
The reason why /\bà/.test("à") doesn't match is because "à" is not a word character. The escape sequence \b matches only between a boundary of word character and a non word character. /\ba/.test("a") matches because "a" is a word character. Because of that, there is a boundary between the beginning of the string (which is not a word character) and the letter "a" which is a word character.
Word characters in JavaScript's regex is defined as [a-zA-Z0-9_].
To match an accented character at the start of a string, just use the ^ character at the beginning of the regex (e.g. /^à/). That character means the beginning of the string (unlike \b which matches at any word boundary within the string). It's most basic and standard regular expression, so it's definitely not over the top.
If you want to match letters, whether or not they're accented, unicode property escapes can be helpful.
/\p{Letter}*/u.test("à"); // true
/\p{Letter}/u.test('œ'); // true
/\p{Letter}/u.test('a'); // true
/\p{Letter}/u.test('3'); // false
/\p{Letter}/u.test('a'); // true
Matching to the start of a word is tricky, but (?<=(?:^|\s)) seems to do the trick. The (?<= ) is a positive lookbehind, ensuring that something exists before the main expression. The (?: ) is a non-capture group, so you don't end up with a reference to this part in whatever match you use later. Then the ^ will match the start of the string if the multiline flag isn't set or the start of the line if the multiline flag is set and the \s will match a whitespace character (space/tab/linebreak).
So using them together, it would look something like:
/(?<=(?:^|\s))\p{Letter}*/u
If you want to only match accented characters to the start of the string, you'd want a negated character set for a-zA-Z.
/(?<=(?:^|\s))[^a-zA-Z]\p{Letter}*/u.match("bœ") // false
/(?<=(?:^|\s))[^a-zA-Z]\p{Letter}*/u.match("œb") // true
// Match characters, accented or not
let regex = /\p{Letter}+$/u;
console.log(regex.test("œb")); // true
console.log(regex.test("bœb")); // true
console.log(regex.test("àbby")); // true
console.log(regex.test("à3")); // false
console.log(regex.test("16 tons")); // true
console.log(regex.test("3 œ")); // true
console.log('-----');
// Match characters to start of line, only match characters
regex = /(?<=(?:^|\s))\p{Letter}+$/u;
console.log(regex.test("œb")); // true
console.log(regex.test("bœb")); // true
console.log(regex.test("àbby")); // true
console.log(regex.test("à3")); // false
console.log('----');
// Match accented character to start of word, only match characters
regex = /(?<=(?:^|\s))[^a-zA-Z]\p{Letter}+$/u;
console.log(regex.test("œb")); // true
console.log(regex.test("bœb")); // false
console.log(regex.test("àbby")); // true
console.log(regex.test("à3")); // false
Stack Overflow had also an issue with non ASCII characters in regex, you can find it here. They are not coping with word boundaries, but maybe gives you anyway useful hints.
There is another page, but he wants to match strings and not words.
I don't know, and did not find now, an anchor for your problem, but when I see what monster regexes in my first link are used, your group, that you want to avoid, is not over the top and to my opinion your solution.
const regex = /^[\-/A-Za-z\u00C0-\u017F ]+$/;
const test1 = regex.test("à");
const test2 = regex.test("Martinez-Cortez");
const test3 = regex.test("Leonardo da vinci");
const test4 = regex.test("ï");
console.log('test1', test1);
console.log('test2', test2);
console.log('test3', test3);
console.log('test4', test4);
Building off of Wak's and Cœur's answer:
/^[\-/A-Za-z\u00C0-\u017F ]+$/
Works for spaces and dashes too.
Example: Leonardo da vinci, Martinez-Cortez
Unicode allows for two alternative but equivalent representations of some accented characters. For example, é has two Unicode representations: '\u0039' and '\u0065\u0301'. The former is called composed form and the latter is called decomposed form. JavaScript allows for conversion between the two:
'é'.normalize('NFD') // decompose: '\u0039' -> '\u0065\u0301'
'é'.normalize('NFC') // compose: '\u0065\u0301' -> '\u0039'
'é'.length // composed form: -> 1
'é'.length // decomposed form: -> 2 (looks identical but has different representation)
'é' == 'é' // -> false (composed and decomposed strings are not equal)
The code point '\u0301' belongs to the Unicode Combining Diacritical Marks code block 0300-036F. So one way to match these accented characters is to compare them in decomposed form:
// matching accented characters
/[a-zA-Z][\u0300-\u036f]+/.test('é'.normalize('NFD')) // -> true
/\bé/.test('é') // -> false
/\bé/.test('é'.normalize('NFD')) // -> true (NOTE: /\bé/ uses the decomposed form)
// matching accented words
/^\w+$/.test('résumé') // -> false
/^(?:[a-zA-Z][\u0300-\u036f]*)+$/.test('résumé'.normalize('NFD')) // -> true

Categories