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
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 6 years ago.
Improve this question
I have two words within sentence.
EX: big BUTTON
here I need to check second word is in uppercase using regex expression.
Square brackets [] are your friend. They allow you to specify characters that will match. To match the first work, you need to check for any letter. This can be done with [a-zA-Z]. This will match any letter between a and z, as well as A and Z. for the second word, you only want to match uppercase, so only use [A-Z]. To get 1 or more matches, put a + after the closing bracket.
Putting this all together, with a space in between the words, you get [a-zA-Z]+ [A-Z]+.
The carat ^ is used to signify the start of the string, and the dollar sign $ is used to signify the end of the string. Your question somewhat vague, so here are a couple scenarios:
Each sentence is only two words with a space in between them: ^[a-zA-Z]+ [A-Z]+$
Each sentence has at least two words and may or may not end in a period: ^[a-zA-Z]+ [A-Z]+( |\.?$)
In the second example the parenthesis with a pipe (|) is used as an OR statement. The period is escaped since it is a special character (matches any single character). The question mark denotes 0 or 1 of the preceding character, which is a period. So ( |\.?$) will match a space or a sentence that ends with or without a period.
Here is a good site that has information on Regexes: http://www.regular-expressions.info/
This regexp looks for any sequence, starting at the beginning of the string (^), of alphanumeric characters (\w)--that's the first word--then a space, followed by a sequence of upper-case letters ([A-Z]+)--the second word--followed by either a space or the end of the string ($).
/^\w+ [A-Z]+( |$)/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am going nuts on this for a few days now. I have read and tried many previously answered questions close to this topic, I've fiddled with all kinds of expressions on regexr.com (very awesome page BTW) but I can't for the life of me figure it out.
I want to do the following in JavaScript/Jquery (this has to do with contenteditable="true" elements where I want to restrict user input).
Some elements are for text input without spaces and allowed numbers (abc1_d-ef), some for free text (abc. d2ef, gh6i? j8kl: mno!), some for integers (123), some for decimals (1,2 / 1.2).
BUT I want to always forbid a newline character or tab (\n \r \t \f).
So:
var pattern = new RegExp(.........);
var text = $("#my-id").html();
var test = pattern.test(text);
// test should be true for correct text / integer / decimal
// but should be false as soon as text contains a newline, tab etc.
So basically I'm looking for 4 different expressions:
letters, numbers, underscore, hyphen
letters, numbers, spaces, special characters and such
numbers 0-9
numbers, comma and dot
but none of them with newline etc.
I hope I could make myself clear.
For all your requirements, you can use:
^[-,.\w ]+$
See a demo on regex101.com.
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 7 years ago.
Improve this question
How do I detect strings in Javascript that end with (number-number) at end of string?
Examples of strings I'm looking to match:
High (0-22)
Low (23-100)
Regex
/.*\(\d+-\d+\)$/
Explanation
. = any character
* = zero or more times
\(,\) = literal parentheses (must escape with backslash or it will be interpreted as a regex control character)
\d = digit
+ = one or more times
- = literal hyphen
$ = end of line
Links
Here is a jsfiddle to play with.
http://www.regular-expressions.info/ has lots of useful information.
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