Matching any character followed by not numbers - javascript

I am not good at RegEx. Though it seems very simple to achieve but I am not able to find out the way to match any characters followed by not numbers. I am trying with negative lookahead. If I use any word it is working as expected but if I try to match any character with square bracket, it is failing.
var pattern = /sample(?!\d)/;
console.log(pattern.test("sample324")); //false
var pattern = /[a-z]+(?!\d)/;
console.log(pattern.test("sample324")); //true but expect false
Thanks in advance.

Problem is that [a-z]+(?!\d) will let it match any 1+ characters not followed by a digit, so it will match sampl in your input satisfying assertion of non-digit at next position.
You may use this regex with a negative lookahead:
/^(?!.+\d)/
This will fail the match if a digit appears anywhere in input after 1+ of any character.
RegEx Demo
For better efficiency, you may use this regex as well:
/^(?!\D+\d)/
Which fails if there 1+ non-digits followed by a digit anywhere in input.

I think this may work:
var pattern = /[^0-9]/.test('mystring9')

Related

RegExp: How to find only one match (or not match pattern)

I can't get how to write regexp right to be able match only heo. So for example if we found some l
char during parsing - cancel that match then.
'heo heo helo'.match(/he.*(?!l)o/gi) // should be only [heo, heo]
UPD:
I need to match as mutch as possible times among the string. Not the first one. Thanks
Example (wrong one):
console.log('heo heo helo'.match(/he.*(?!l)o/gi))
There are two issues:
.* - matches any zero or more chars other than line break chars as many as possible, and thus will match till the last occurrence of the subsequent patterns in the regex. You might use a non-greedy .*? here to fix the issue.
(?!l)o - always matches o, since o is not l, (?!l), a negative lookahead, always returns true, saying, yes, go ahead and return the match. You wanted a negative lookbehind, (?<!l) here.
To match strings starting with he and then matching any chars (other than line break chars) as few as possible and then o not preceded with l, you can use
/he.*?(?<!l)o/gi
See this regex demo. The .*?(?<!l)o pattern matches any 0+ chars other than line break chars as few as possible up to the leftmot o that is not immediately preceded with l.
Now, if you just want to match words that start with he and end with o not preceded with l, you can use
/\bhe[a-z]*(?<!l)o\b/gi
/\bhe(?![a-z]*lo\b)[a-z]*o\b/gi
See this regex demo and this regex demo.
console.log('heo heo helo'.match(/he.*(?!l)o/gi))
You matches any characters .* before checking the condition (?!l). Your regex should check condition before matching characters.
Besides, you want to match only hexxxo (x is not l), so you should use \b in your regex. I suggest following regex.
console.log('heo heo helo aheob'.match(/\bhe[^l]*o\b/gi));

RegEx for single asterisk (*) and alphanumeric

I need a javascript regex that accepts any alphanumeric character (can be any amount of characters or 0 characters if an asterisk is present) and a single asterisk anywhere in the string (but it does not need the asterisk).
Matches
*
abc
*abc
abc*
a*bc
Invalid Matches
**
*_abc
*abc*
abc**
**abc
I have
^([A-Za-z\d*]?)+$
but that matches multiple asterisks and I'm not sure how to only allow one https://regex101.com/r/a1C9bf/1
You may use this regex with a negative lookahead:
/^(?!(?:.*\*){2})[A-Za-z\d*]+$/gm
Updated RegEx Demo
Negative lookahead (?!(?:.*\*){2}) fails the match if there are more than one * in input.
Without requiring any look-ahead, you could use ^([\da-zA-Z]+|[\da-zA-Z]*\*[\da-zA-Z]*)$
https://regex101.com/r/xW2IvR/2
You could do:
^(?=.)[A-Za-z\d]*\*?[A-Za-z\d]*$
This will match any string that that's at least one character long ((?=.)), starts with zero or more alphanumeric characters, contains an optional *, and ends with zero or more alphanumeric characters.
You could also replace [A-Za-z\d] with [^\W_] to make it a little shorter (but slightly harder to read):
^(?=.)[^\W_]*\*?[^\W_]*$
You want one match one of two possible cases:
an asterisk surrounded by zero or more alphanumeric characters
one or more alphanumeric characters
Then this is your regex:
^([a-zA-Z\d]*\*[a-zA-Z\d]*|[a-zA-Z\d]+)$

How to write a regex which matches anything but a number

I am trying to create an angular directive to be used with HTML inputs to filter out none numeric characters
Here is my regex I am using to achieve that:
inputValue.replace(/[^0-9\-\.]/g, "").replace(/\.(\.)/g, '$1');
However this regex does not cover these cases:
--5
5.5.6
-5-5
If I'm not wrong, this is really simple.^^
\d
\d machtes every digit from [0-9]. You can test our RegEx very simple at https://regex101.com without writing any javascript code to test it.
Edit:
You might want to add a * to the \d.
\d*
The * is the greedy selector, which selects all of the type before.
In your regex you use a negated character class [^0-9\-\.] which matches not a digit 0-9, a - and a . so you are keeping those matches.
If you want to match anything but a number you could use [^0-9] or \D to match any character that is not a digit and replace that with an empty value.
let inputValue = `--5
5.5.6
-5-5
!##$%# $%#% $%435 452545`;
inputValue = inputValue.replace(/\D/g, "");
console.log(inputValue);

Regex JavaScript - how to check if a string is all letters and then all numbers

This would be ok: 'AAAAAAA1222222'
This would be not ok: '1AAAAA'
This would not be ok: 'AA1AA'
Just looking for a way to check if a string is ALL letters and then ONLY letters afterward.
This is an easy one.
^[A-Za-z]*[0-9]*$
That of course is assuming that no letters is OK.
For example, the above example would match
AAAAAAA
2222222
as well as an empty string.
If there must be at least one letter and at least one number, replace the * with +
^[A-Za-z]+[0-9]+$
text.match(/^[A-Z]*\d*$/i)
Read this as "start of string followed by any number of letters followed by any number of digits followed by the end of the string."
Note this will match "", "A", and "1". If you want there to be at least one letter and at least one number, use + instead of * in both spots.
Use a lookahead. Lookaheads are used for validation, I suggest you go through this.
Try this out: ^(?=[A-Za-z]*\d*$).+
DEMO
try this regex
\b\D+\d+\b
\b is the word boundary that won't allow for digits to come in the beginning and letters to come after the digits at the end.
I suggest something like this:
text.match(/\b\D+\d+[^\D]\b/);
The \b was suggested by Grace and is a good idea. Another option is to use the beginning and end anchors like:
text.match(/^\D+\d+[^\D]$/);
text.match(/^[a-zA-Z]+\d*$/);
Tests:
AAAAAAA1222222 - match
1AAAAA - no match
AA1AA - no match
AAAAAAA - match
2222222 - no match
If you dont want to match ALL Letters and at least one number change the quantifier of \d from +(1-infinite times) to *(0-infinity times)
more about regex quantifiers : link

Regular Expression for alphabets with spaces

I need help with regular expression. I need a expression which allows only alphabets with space for ex. college name.
I am using :
var regex = /^[a-zA-Z][a-zA-Z\\s]+$/;
but it's not working.
Just add the space to the [ ] :
var regex = /^[a-zA-Z ]*$/;
This is the better solution as it forces the input to start with an alphabetic character. The accepted answer is buggy as it does not force the input to start with an alphabetic character.
[a-zA-Z][a-zA-Z ]+
This will allow space between the characters and not allow numbers or special characters. It will also not allow the space at the start and end.
[a-zA-Z][a-zA-Z ]+[a-zA-Z]$
This will accept input with alphabets with spaces in between them but not only spaces. Also it works for taking single character inputs.
[a-zA-Z]+([\s][a-zA-Z]+)*
Special Characters & digits Are Not Allowed.
Spaces are only allowed between two words.
Only one space is allowed between two words.
Spaces at the start or at the end are consider to be invalid.
Single word name is also valid : ^[a-zA-z]+([\s][a-zA-Z]+)*$
Single word name is in-valid : ^[a-zA-z]+([\s][a-zA-Z]+)+$
Regular expression starting with lower case or upper case alphabets but not with space and can have space in between the alphabets is following.
/^[a-zA-Z][a-zA-Z ]*$/
This worked for me
/[^a-zA-Z, ]/
This will work too,
it will accept only the letters and space without any symbols and numbers.
^[a-zA-z\s]+$
^ asserts position at start of the string Match a single character
present in the list below [a-zA-z\s]
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z matches a single
character in the range between a (index 97) and z (index 122) (case
sensitive) A-z matches a single character in the range between A
(index 65) and z (index 122) (case sensitive) \s matches any
whitespace character (equivalent to [\r\n\t\f\v ]) $ asserts position
at the end of the string, or before the line terminator right at the
end of the string (if any)
This worked for me, simply type in javascript regex validation
/[A-Za-z ]/
This one "^[a-zA-Z ]*$" is wrong because it allows space as a first character and also allows only space as a name.
This will work perfectly. It will not allow space as a first character.
pattern = "^[A-Za-z]+[A-Za-z ]*$"
This works for me
function validate(text) {
let reg = /^[A-Za-z ]+$/; // valid alphabet with space
return reg.test(text);
}
console.log(validate('abcdef')); //true
console.log(validate('abcdef xyz')); //true
console.log(validate('abc def xyz')); //true
console.log(validate('abcdef123')); //false
console.log(validate('abcdef!.')); //false
console.log(validate('abcdef#12 3')); //false
This will restrict space as first character
FilteringTextInputFormatter.allow(RegExp('^[a-zA-Z][a-zA-Z ]*')),
This will work for not allowing spaces at beginning and accepts characters, numbers, and special characters
/(^\w+)\s?/

Categories

Resources