explanation of regex rule [closed] - javascript

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I've inherited some code which validates a coupon.
The logic of this coupon is a regular expression rule verified with JS.
I don't have much experience with this, and will really appreciate if someone can help me understand it.
This is the rule:
theStr.match('^[a-z]{1}[0-9]{3}[a-z]{1}$')

Must start with a lowercase letter followed by three numbers and another lowercase letter. Also note that in JS you use /regex/ not 'regex'.

This site can be very helpful for explaining regexes: http://regex101.com/
In this case, that regex matches the following, in this order:
beginning of a string/line
any lower case letter
repeated only once (no repetitions)
any digit
repeated exactly 3 times
any lower case letter
repeated only once
end of string/line
This would match a string like "b589n".

Line starts with one lowercase letter a-z, followed by three digits, and ending with one lowercase letter a-z
The ^ symbol means "at the start of the line", the $ symbol means "at the end of the line", the stuff in the []'s is the chars to match on, and the number in the {}'s is the number of times to repeat.

Related

Regex for password with specific restrictions [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Regular expression to check if password is "8 characters including 1 uppercase letter, 1 special character, alphanumeric characters"
(14 answers)
Closed 3 years ago.
I'm trying to come up with a regex that validates passwords. The restrictions are as follows:
Must be at least two of the following:
one lowercase [a-z],
one uppercase [A-Z],
one digit [\d],
one special character [!##\$%\^\&*)(+=._-].
must not begin or end with white-space but can contain white-spaces inside,
must be between 7 and 20 characters long.
So far, this is the last version of what I've come up with:
^(?=.{7,20}$)(?:(?=.*[\d!##\$%\^\&*\)\(+=._-])(?=.*[a-z])\S*|(?=.*[A-Z])(?=.*[\d!##\$%\^\&*\)\(+=._-])\S*|(?=.*[A-Z])(?=.*[a-z])\S*|(?=.*[\d)\(+=._-])(?=.*[!##\$%\^\&*\)\(+=._-])\S*)$
This works for all of the above except letting white-spaces inside. I've gone through multiple regex and this is the best one so far (but also the ugliest).
Edit: Thank you for the fast replies. Why these requirements are in place is beside the point. I know passwords would be more secure if all of the above were required. But as not all customers use password managers...
Now, why is this not a duplicate question? Because no other thread requires any two of the above. They simply start with requiring specific two, than adding another one and so on. This needs to be any two conditions.
Hey you can use below regex to fulfill your requirement
^(?=.\d)(?=.[A-Z])(?=.[a-z])(?=.[^\w\d\s:])([^\s]){7,20}$

Match a character followed by another [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I want to find a . and get any characters after I found a (, with a regex. How do i make that happen?
I would also like to see some good tutorials on regexes for Javascript.
Following regex should work for you:
[.]([^(]*)[(]
Text you want to capture will be available in group # 1.
Javascript Code:
var str='here is a sentance. and some other text ( here )';
var match = str.match(/[.]([^(]*)[(]/);
console.log(match[1]); // and some other text
Live Demo: http://www.rubular.com/r/ALqusiC9EQ
It seems like you're having trouble with the concept of escaping. The . and ( characters have special meaning in RegEx, so you need to escape them by placing a \ in front of them. For example, to match a literal dot, you might use \.
For repetition, you can use * or + for 0+ and 1+ respectively. These are used as modifiers on preceding expressions. So, for example, A+ means "one or more A characters", whereas A* means "zero or more A characters". You can also use the ? modifier to alter the "greedy" behavior of these matches, but that's a more complicated topic.
If you need to constrain the exact number of repetitions, you can use the {n} syntax. For example, you might use A{10} to match exactly 10 A characters, or A{3,5} to match between 3 and 5 A characters.
These also work on groups and classes, e.g. [A-Z]{3} or (a*b+){3}.
As far as RegEx tutorials go, pretty much nowhere beats Regular-Expressions.info, though the MDN article on RegEx might be useful on the JavaScript side of things too.

Need help creating javascript regexp [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have absolutly no experience with regular expressions and I need some help setting up one to match a string with. This is for phone number validation. I need to make sure that a string a user inputs has only upper case letters A-Z, numbers 0-9, open/close parentheses[()], and hyphens(-). I also don't know what string method I need to use either match or string.
RegEx is explained poorly all over the web. I don't fault anyone for asking more general questions about it and this is different from the other post which is more do-it-form-me google-evasion than specific question. The characters you asked about:
[A-Z]
[0-9] or \d
\(
\)
-
/matchme/ is a regular expression literal. This is preferable to useing the RegExp constructor because you end up having to escape your escape backslashes which gets real ugly.
You can actually use regEx literals in a lot of string methods, like replace, split, etc.
Without special characters following, any non-special character is about matching one character at that position in a string. Stuff in [] is a class and can match more than one KIND of character but only the character at that positions following the last position matched. You might [.- ] useful for identifying non-number characters for telephone numbers. You can also express ranges in character classes, e.g. [a-hA-H] or [4-9]
But one str position at a time goes out the window when you start using the follow-up characters:
? - one or none
* - 0 or many
+ - 1 or more
Avoid the . wildcard character. It is inefficient. For some reason that I suspect goes down all the way to implementation in assembly for efficiency's sake, it checks against every single possibility rather than the 1-2 teletype whitespace characters it actually doesn't represent and there is no honest use for on a computer. More importantly, the better-performing alternative is much more powerful and helpful. Negating character classes are much faster. [^<]* represents 0 or more positions of anything that is NOT a < character.
Very handy stuff for XML/SGML-style parsing which in spite of what many on Stack have said, is perfectly feasible with regEx, which is no longer technically confined to "regular" languages. You have to be aware of what your looking with something that allows as much sloppiness as somebody else's HTML but that's just a 'duh' in my book.
Crockford warns against negating character classes in JSlint. Crockford is painfully wrong on that count. They are not only much more efficient, they also make it much easier to think through how to tokenize stuff. If there is a security risk, you can set explicit limits to the number of characters matched with {} brackets, e.g. p{2,5} - which matches two to five p chars or {5} for exactly 5 or {,5} for up to 5 or {5,} at least 5 (I think - test those last two)
Other random stuff you should look up:
(ph|f) - ph or f - helpful for finding phish and fish (when a class won't do, basically)
^ - represents beginning of a string - think of as a condition for the next character more than a character itself. Yes, it also negates character classes.
$ - represents end of a string - same caveat as above but on the previous character.
\ - used to escape special symbols. Note: a lot of special symbols that have no meaning in character classes require no \ inside []
\s\w\d - These represent commonly used sets of characters. The first is pretty much all whitespace (js-style escapes typically have regEx equivalents) followed by w for word characters (class equivalent [a-zA-Z0-9_]) and d for digits [0-9]. Capitalize any of these for the exact opposite.
There's more, like back-references, and lookaheads whose use-case scenarios are worth knowing but this is the commonly used stuff I actually remember from regular experience (bwaahaahaa).
I assume you're looking for non US since you have that A-Z concern and I'm sure there's plenty of US phone-numbers regExes out there but I'd probably do something like this for US numbers:
/\(?\d{3}[)\-. ]?\d{3}[\-. ]?\d{4}/
to match:
123-456-7890
(123)456-7890
123.456.7890
123 456 7890
1234567890
But also perhaps messily allows:
(123456.7890
...which I'm willing to live with for the sake of avoiding complexity. Resist the temptation to do it all with one expression. Sometimes it's much cleaner to eliminate trailing/leading whitespace for instance, and then hit something with an expression. Split and join methods are very powerful for tokenizing
If this goes like a usual regEx conversation, somebody will shortly point out something I missed in my pattern. So yeah, test 'em out on stuff. There's sites that let you set the expression and then just plug in characters to try and break them.

regular expression to extract fraction, decimal and number from a string [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need a regex to extract fraction, decimal and integer values from a given string. For example, an input will be like following.
1/2 apples
1.5 orange
2.5 orange
1 lemon
And output should be following.
1/2
1.5
2.5
1
I figured out how to extract fraction, decimal and integer values separately using 3 different regex, but I can't figure out a way to extract all the possible number values with one regex.
I'm currently using Javascript String object's match method.
Any help will be appreciated.
Use regex pattern
\d+([/.]\d+)?
...which leads to Javascript code
var n = str.match(/\d+([\/.]\d+)?/g);
Check this fiddle/demo.
Then why not present your working regexes?
But here is how, simply use an alternation:
\d+(\/\d+|\.\d+)?
This is already slightly optimized. You could simply separate your three regexes by |.
The easiest possible regular expression in this case (assuming that your numbers always in the beginning of the string) is:
^\S+
Examples:
'1/2 apples'.match(/^\S+/) // ["1/2"]
'1 lemon'.match(/^\S+/) // ["1"]
If you are sure that all strings have the same format (i.e. starts with amount, then space, then text), there is even no need in regular expression:
"1/2 apples".split(" ").shift(); // gives "1/2"

Translate this JavaScript Gibberish please? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm trying to modify and update an old Greasemonkey script with the goal of automatically adding an affiliate ID to all Amazon links. I'm a novice when it comes to JavaScript, but I'm usually pretty good about modifying existing scripts in any language. There's just one line here that I can't wrap my head around.
The script I started with is outdated, so I don't know if there is a problem with the syntax or if the link format has changed. Can somebody please help me understand what this line is doing so I can make changes to it?
const affiliateLink = /(obidos.(ASIN.{12}([^\/]*(=|%3D)[^\/]*\/)*|redirect[^\/]*.(tag=)?))[^\/&]+/i;
Alright, you asked for it :)
Start the regular expression:
/
Start a group operation:
(
Search for the text "obidos" followed by any single character
obidos.
Open another group operator:
(
Search for the text "ASIN" followed by any 12 characters
ASIN.{12}
Another group operation:
(
Followed by 0 or more characters that are not slashes:
[^\/]*
Group operation searching for an '=' character or a url encoded '=' (%3D):
(=|%3D)
Followed by 0 or more characters that are not slashes:
[^\/]*
Followed by slash (and closes the current group), which can be repeated 0 or more times:
\/)*
Allows the pattern to match if the previous group was found OR everything to the right of the bar is matched:
|
Matches the text "redirect" followed by 0 or more chatacters that are not a slash:
redirect[^\/]*
Matches any single character, followed optionally by the text "tag=":
.(tag=)?
Closes the two group operations we're currently still inside of:
))
Followed by one or more characters that are not a slash or &:
[^\/&]+
Closes the regular expression:
/
Download a copy of expresso, its a great utility for this and comes in handy for all this stuff. then just place the regex into that (everything between the starting slashes and ending slash).
I would describe what string it matches e.c.t. but its fairly complex as theres lots of components to it. Its easier for you to look at it yourself. expresso provides a more english explanation of each pattern

Categories

Resources