Regex matching /xx/xx/ or /xx/xx - javascript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
What is the regular exression which will match the string /xx/xx/ as well as /xx/xx. xx can be any two letters.

The simpelest solution would be a literal pattern of "slash, 2 chars, slash, 2 chars, optional slash":
/\/\w\w\/\w\w\/?/
Structure:
/ //JS's "Start regex"
\/ // Escaped "Slash" (matches / )
\w // Any word character ([A-Za-z0-9_]) (Twice)
// Then a slash and 2 \w's
\/? // Optional slash.
Usage:
"/xx/xx/".match(/\/\w\w\/\w\w\/?/) // ["/xx/xx/"]
"/xx/xx".match(/\/\w\w\/\w\w\/?/) // ["/xx/xx"]
"/xx/x".match(/\/\w\w\/\w\w\/?/) // null
You could make the regex a little shorter:
/(\/\w\w){2}\/?/
Also, you may want to replace \w with [A-z], since \w also matches digits ([0-9]) and the underscore character (_). If you want the regex to only allow letters, use [A-z].

Hope below would help:
\/[a-zA-Z][A-Za-z]\/[A-Za-z][A-Za-z](\/)?/
Assuming x could be caps or small letters.
Test Samples:
Ab/cd --> True
AA/BB --> True
A/B --> False
XX/SS/ --> True
/X/x/ --> False

Related

What is JavaScript regex for string matches which starts with $$ and ends with $$? [closed]

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

Regular Expression in Java Script [closed]

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

Regular Expression for SKU validation [closed]

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

TITLE VALIDATION in JavaScript for REGEXP which can take alphanumeric and can take Special char [, / ( ) & - : . space] [closed]

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

Regex to accept alphanumberic, whitespace and - in Javascript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What would be the Regex to accept alphanumeric, whitespace and - in Javascript.
"HI THERE 0-9 are valid characters".match(/[a-z0-9\s-]+/i)
a-z will match your favourite alphabet (the roman one!)
0-9 will match your favourite numerals (the arabic ones!)
\s will (probably) match your favourite whitespace characters (\f, \n, \r, \t, \v, \u00A0, \u2028, and \u2029)
-, since it's at the end of the set, will match a literal -.
The i modifier at the end of the regex makes it case-insensitive, so a-z will also match A-Z.

Categories