Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I do not know if this is nonsense but I need a regular expression to validate the SKU of a product, taking into account that it must be 10 characters minimum and 20 maximum and allow [0-9] and [a-zA-Z] but I have not the slightest idea, the regular expression is to be used from Javascript with . test(), any help?
You can use character class and specify length range for this.
A character class is a set of characters (or character range) enclosed in a pair of square brackets like [abc] or [A-Z].
So for your case, it would be [A-Za-z\d] which denotes an upper case or lower case letter or a digit. \d stands for digit.
For allowing n occurrence of any pattern, you can use <your_pattern>{n}. If it is a range from m to n, use <your_pattern>{m,n}. For specifying only minimum, use <your_pattern>{n,} and for specifying only maximum, use <your_pattern>{,n}.
Using a service for regex is helpful.
This should be what you need
^[a-z0-9A-Z]{10,20}$
if it is just JavaScript you can set a flag for upper and lower case
/^[a-z0-9]{10,20}$/i
Example below.
http://regex101.com/r/uE8fH1
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
I have the following regex that I will use in Javascript eventually:
^(0|[1-9][0-9]*)([,;|\t]|(?:x) | {2,})
Given this sample text, I would like to get on the capture group 1 the quantity and on the capture group 2 the separator (either a comma, semicolon, pipe, tab, two or more spaces or a "x" followed by a single space):
2,Apples
3x Trees
2 Oranges
2x A3252893
15553x Moons
15553x Apples Test test
Regex101 example
Now, the question is about me wanting to match the single space as separator only if it is preceded by the "x" character, i.e. basically leaving out the "x" from the group. If I change the regex above by replacing the non-capturing group with the positive lookbehind, it doesn't work:
^(0|[1-9][0-9]*)([,;|\t]|(?<=x) | {2,})
Regex101 updated example
Why doesn't it match? Is that because it is used inside an alternation? And how can I fix that?
EDIT: OK, so positive lookbehind is not the correct solution because I didn't match any x before that. So how can I match the separators that I've listed above on the same numbered capturing group 2 without including the x but only the space (i.e. capture the space only if its preceded by an x while keeping the other variants as they are)?
The problem is that the look behind assertion for an "x" comes at a moment that the digits have been matched, so the previous character will always be a digit, not "x".
You can solve this by reading the "x" first (when it is there), before continuing. So insert x?
^(0|[1-9]\d*)x?([,;|\t]|(?<=x) | {2,})
On regex101
If you don't want to allow the "x" to be there when another delimiter (or two spaces) is used, then use a negative look behind assertion in those cases:
^(0|[1-9]\d*)x?((?<!x)[,;|\t]|(?<=x) |(?<!x) {2,})
On regex101
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 8 months ago.
Improve this question
I am trying to figure how I can search for a pattern that gives me only digits or digits followed by only one letter. I know I can use /\D\g to find only digits but I dont know how to find digits with only one letter after it. The letters can only be the following:['a','A','b','B','c','C','d','D','n','N','e','E','s','S','w','W']
const testPattern = /[A-Za-z][0-9]/
console.log('item_10a_object10a'.pattern(testPattern))
First, you need to group the whole thing with (), and end with /g so it matches multiple groups.
If you want digits first then a number, you need to put the letter block after the numbers: ([0-9][A-Za-z])
If you want multiple digits to match, you need a + after the numbers block: [0-9]+
All together: /([0-9]+[A-Za-z])/g
For reference, \d does the same thing as [0-9], so you could do /(\d+[A-Za-z])/g instead
LPT: use regex101 to build and test regex queries
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want Javascript regex for all strings that start with $$ and ends with $$
example:
$$hello$$
$$world$$
^\${2}.*?\${2}$
How this works: first, you look for two dollar signs, then, it allows any input, and then it looks whether the string ends with two dollar signs.
A pattern like this should work:
^\$\$.*\$\$$
This will match the beginning of the string (^) followed by $$, followed by zero or more of any character, followed by $$, and the end of the string ($). For example:
/^\$\$.*\$\$$/.test('$$hello$$') // true
If you're looking for a substring of a larger string which matches this pattern, use something like this:
\$\$.*?\$\$
This will match $$, followed by zero or more of any character, non-greedily, followed by $$. For example:
/\$\$.*?\$\$/.exec('print "$$hello$$"') // ['$$hello$$']
But for something this simple, you can just use plain old string manipulation:
var string = '$$hello$$';
var result = (string.substr(0, 2) == '$$') && (string.substr(-2) == '$$'); // true
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am not able to understand this pattern. What does this Regex mean?
/([^0-9])\d{1,4}([^0-9])/g
This regex will:
/ /* delimeter */
([^0-9]) /* capture non-digit character */
\d{1,4} /* match 1 to 4 digits */
([^0-9]) /* capture 1 non-digit character */
/g /* multiple times in the string */
PS: [^0-9] is identical to [^\d] also identical to \D
It's matching a String which contains the following:
some character which is not a digit
followed by 1 to 4 digits
followed by a character which is not a digit
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my website , i need a REGEXP in java script for validation TITLE which can take alphabates, digits and Special char set [, / ( ) & - : . space], but if any user enter only single and double spaces or single or double .. like [..] in title or double digit [1 2] then it's should not allowed, atlest one aplhabate is required. please help
You can use this pattern:
^[-a-z0-9,/()&:. ]*[a-z][-a-z0-9,/()&:. ]*$
This will match any number of your special characters followed by a Latin letter, followed by number of your special characters. It's effectively equivalent to [-a-z0-9,/()&:. ]+ except it requires at least one [a-z] somewhere in the string.
Of course, you need to escape the \ when written as a regex literal in javascript, and you probably want to use the i flag for case-insensitive matching:
var pattern = /^[-a-z0-9,\/()&:. ]*[a-z][-a-z0-9,\/()&:. ]*$/i