Testing string for words with 3 repeated consecutive characters - javascript

I'm trying to get a Regex expression that will be satisfied when given a string that has at least 1 word composed of 3 or more repeating consecutive characters, and no other characters:
Testing AAAAAA Test - Valid
Testing AAAAAAB Test - Invalid
The previous solution I had reached was not enough to recognize if there were different characters in the word:
/^(?:(.)(?!\1{2}))+$/gi
This was essentially just testing if the 2 characters after each character are equal to it.
Any ideas?

How about
\b(.)\1{2,}\b
which is
\b word boundary
(.) something
\1 previous thing...
{2,} ...twice or more
\b word boundary
https://regex101.com/r/BKHkOc/3

Add word boundaries. And use {2,} to match at least 2 repetitions.
/\b(.)\1{2,}\b/
There's no need for i, since you're not matching letters, so case is irrelevant. And g is not needed when just testing; it's only useful if you're returning all the matches or doing a replacement.

Related

Regex for match beginning with 2 letters and ending with 3 letters

Example input:
'Please find the ref AB45676785567XYZ. which is used to identify reference number'
Example output:
'AB45676785567XYZ'
I need a RegExp to return the match exactly matching my requirements; i.e. the substring where the first 2 and last 3 characters are letters.
The first 2 and last 3 letters are unknown.
I've tried this RegExp:
[a-zA-Z]{2}[^\s]*?[a-zA-Z]{3}
But it is not matching as intended.
Your current RegExp matches the following words marked with code blocks:
Please find the ref AB45676785567XYZ. which is used to identify reference number
This is because your RegExp, [a-zA-Z]{2}[^\s]*?[a-zA-Z]{3}, is asking for:
[a-zA-Z]{2} Begins with 2 letters (either case)
[^\s]*? Contains anything that isn't a whitespace
[a-zA-Z]{3} Ends with 3 letters (either case)
In your current example, restricting the letters to uppercase only would match only the match you seek:
[A-Z]{2}[^\s]+[A-Z]{3}
Alternatively, requiring numbers between the 2 beginning and 3 ending letters would also produce the match you want:
[a-zA-Z]{2}\d+[a-zA-Z]{3}
What is really important here, is word boundaries \b, try: \b[a-zA-Z]{2}\w+[a-zA-Z]{3}\b
Explanation:
\b - word boundary
[a-zA-Z]{2} - match any letter, 2 times
\w+ - match one or more word characters
[a-zA-Z]{3} - match any letter, 3 times
\b - word boundary
Demo
CAUTION your requirements are amibgious, as any word consisting of 5 or more letters would match the pattern
Start with 2 letters :
[a-zA-Z]{2}
Digits in the middle :
\d+
Finish with 3 letters :
[a-zA-Z]{3}
Full Regex :
[a-zA-Z]{2}\d+[a-zA-Z]{3}
If the middle text is Alpha-Numeric, you can use this :
[A-Z]{2}[^\s]+[A-Z]{3}

Regex not to allow to consecutive dot characters and more

I am trying to make a JavaScript Regex which satisfies the following conditions
a-z are possible
0-9 are possible
dash, underscore, apostrophe, period are possible
ampersand, bracket, comma, plus are not possible
consecutive periods are not possible
period cannot be located in the start and the end
max 64 characters
Till now, I have come to following regex
^[^.][a-zA-Z0-9-_\.']+[^.]$
However, this allows consecutive dot characters in the middle and does not check for length.
Could anyone guide me how to add these 2 conditions?
You can use this regex
^(?!^[.])(?!.*[.]$)(?!.*[.]{2})[\w.'-]{1,64}$
Regex Breakdown
^ #Start of string
(?!^[.]) #Dot should not be in start
(?!.*[.]$) #Dot should not be in start
(?!.*[.]{2}) #No consecutive two dots
[\w.'-]{1,64} #Match with the character set at least one times and at most 64 times.
$ #End of string
Correction in your regex
- shouldn't be in between of character class. It denotes range. Avoid using it in between
[a-zA-Z0-9_] is equivalent to \w
Here is a pattern which seems to work:
^(?!.*\.\.)[a-zA-Z0-9_'-](?:[a-zA-Z0-9_'.-]{0,62}[a-zA-Z0-9_'-])?$
Demo
Here is an explanation of the regex pattern:
^ from the start of the string
(?!.*\.\.) assert that two consecutive dots do not appear anywhere
[a-zA-Z0-9_'-] match an initial character (not dot)
(?: do not capture
[a-zA-Z0-9_'.-]{0,62} match to 62 characters, including dot
[a-zA-Z0-9_'-] ending with a character, excluding dot
)? zero or one time
$ end of the string
Here comes my idea. Used \w (short for word character).
^(?!.{65})[\w'-]+(?:\.[\w'-]+)*$
^ at start (?!.{65}) look ahead for not more than 64 characters
followed by [\w'-]+ one or more of [a-zA-Z0-9_'-]
followed by (?:\.?[\w'-]+)* any amount of non capturing group containing a period . followed by one or more [a-zA-Z0-9_'-] until $ end
And the demo at regex101 for trying

Regex allow multiple work in a sentence

I'm trying to parse following sentences with regex (javascript) :
I wish a TV
I want some chocolate
I need fire
Currently I'm trying : I(\b[a-zA-Z]*\b){0,5}(TV|chocolate|fire) but it doesn't work. I also made some test with \w but no luck.
I want to allow any word (max 5 words) between "I" and the last word witch is predefined.
To account for non-word chars in-between words, you may use
/I(?:\W+\w+){0,5}\‌​W+(?:TV|chocolate|fir‌​e)/
See the regex demo
The point is that you added word boundaries, but did not account for spaces, punctuation, etc. (all the other non-word chars) between "words".
Pattern details:
I - matches the left delimiter
(?:\W+\w+){0,5}\‌​W+ - matches 0 to 5 sequences (due to the limiting quantifier {n,m}) of 1+ non-word chars (\W+) and 1+ word chars after them (\w+), and a \W+ at the end matches 1 or more non-word chars that must be present to separate the last matched word chars from the...
(?:TV|chocolate|fir‌​e) - matches the trailing delimiter
You need to add the whitespace after the I. Otherwise it wouldn´t capture the whole sentence.
I(\b[a-zA-Z ]*\b){0,5}(TV|chocolate|fire)
I greate site to test regex expressions is regexr
If you don't care about the spaces, use:
/I(\s[a-zA-Z]*\s?){0,5}(TV|chocolate|fire)/
Try
/I\s+(?:\w+\s+){0,5}(TV|chocolate|fire)/
(Test here)
Based on Stefan Kert version, but rely on right side spaces of each extra word instead of word boundaries.
It also accepts any valid "word" (\w) character words of any length and any valid spacing character (not caring for repetitions).

Javascript RegExp non sequential characters

I have this rule
var reg = new RegExp('[a-z]{3}');
Which means it is allowed to use characters between a-z and at least 3 occurrences.
So, I am wondering if there is a way to match this rule with non sequential characters.
In other words,
"abc" => valid
"aaa" => not valid
Thank you!
Here is a working regex for exactly 3 (or N) characters, if the number is not fixed it gets more complicated:
^([a-z])(?!\1{2})[a-z]{2}$
1 2 3 4 5 6 7 8
Explanation:
^ matches the beginning of the string
([a-z]) match one of the accepted characters and save it (group 1)
(?!...) negative lookahead, what is in those brackets is not accepted
\1 reference to the first group (first character here)
{2} repeated exactly twice
[a-z] the accepted characters
{2} repeated exactly twice
$ matches the end of the string
Link here (I added the gm modifiers, so that several expressions can be tested.)
Try to use the excluding lookahead (?![a-z]{3}), it will not match 3 equal characters in sequence.

Regex to match card code input

How can I write a regex to match strings following these rules?
1 letter followed by 4 letters or numbers, then
5 letters or numbers, then
3 letters or numbers followed by a number and one of the following signs: ! & # ?
I need to allow input as a 15-character string or as 3 groups of 5 chars separated by one space.
I'm implementing this in JavaScript.
I'm not going to write out the whole regex for you since this is homework, but here are some hints which should help you out:
Use character classes. [A-Z] matches all uppercase. [a-z] matches all lowercase. [0-9] matches numbers. You can combine them like so [A-Za-z0-9].
Use quantifiers like {n} so [A-Z]{3} gives you 3 uppercase letters.
You can put other characters in character classes. Let's say you wanted to match % or # or #, you could do [%##] which would match any of those characters.
Some meta-characters (characters which have special meaning in the context of regular expressions) will need to be escaped like so: \$ (since $ matches the end of a line)
^ and $ match the beginning and end of the line respectively.
\s matches white-space, but if you sanitize your input, you shouldn't need to use this.
Flags after the regex do special things. For example in /[a-z]/i, the i ignores case.
This should be it:
/^[a-z][a-z0-9]{4} ?[a-z0-9]{5} ?[a-z0-9]{3}[0-9][!&#?]$/i
Feel free to change 0-9 and [0-9] with \d if you see fit.
The regex is simple and readable enough. ^ and $ make sure this is a whole match, so there aren't extra characters before or after the code, and the /i flag allows upper or lower case letters.
I would start with a tutorial.
Pay attention to the quantifiers (like {N}) and character classes (like [a-zA-Z])
^[a-zA-Z][a-zA-Z0-9]{4} ?[a-zA-Z0-9]{5} ?[a-zA-Z0-9]{3}[\!\&\#\?]$

Categories

Resources