i have this regex:
^https?:path\?id=([a-zA-Z0-9._]+)&?.*&gl=([^&|\n|\t\s]+)&?.*$
the query parameter:
?id=([a-zA-Z0-9._]+)&?.*&gl=([^&|\n|\t\s]+)&?.*$
how do i make "gl" to be optional?
You can use
^https?:path\?id=([^&]+)(?:.*?(?:&gl=([^&\s]+).*)?)?$
See the regex demo.
Details:
^ - start of string
http - a fixed http string
s? - an optional s char
:path\?id= - a fixed :path?id= string
([^&]+) - Group 1: one or more chars other than a & char
(?:.*?(?:&gl=([^&\s]+).*)?)? - an optional sequence of
.*? - any zero or more chars other than line break chars as few as possible
(?:&gl=([^&\s]+).*)? - an optional sequence of
&gl= - a fixed string
([^&\s]+) - Group 2: one or more chars other than whitespace and &
.* - any zero or more chars other than line break chars as many as possible
$ - end of string.
Related
I'm trying to limit input data.
My goal:
only two symbols per input allowed: numbers and a comma
first symbol only number (zero or more)
amount of numbers is unlimited (zero or more)
a dangling comma is allowed but only one
Test cases:
1,2,4 - ок
1221,212,4121212 - ок
,2,3 - not ок
1,2,3, - ок
11,21111,31111, - ок
I've hade something like this but it doesn't work properly
/^\d*(,\d*)*$/.test(value)
Appreciate any help!
You can use
/^(?:\d+(?:,\d+)*,?)?$/
See the regex demo. Details:
^ - start of string
(?:\d+(?:,\d+)*,?)? - an optional non-capturing group:
\d+ - one or more digits
(?:,\d+)* - zero or more sequences of a comma and one or more digits
,? - an optional comma
$ - end of string.
I have two patterns for javascript:
/^[A-z0-9]{10}$/ - string of exactly length of 10 of alphanumeric symbols.
and
/^\d+$/ - any number of at least length of one.
How to make the expression of OR string of 10 or any number?
var pattern = /^([A-z0-9]{10})|(\d+)$/;
doesn't work by some reason. It passes at lest
pattern.test("123kjhkjhkj33f"); // true
which is not number and not of length of 10 for A-z0-9 string.
Note that your ^([A-z0-9]{10})|(\d+)$ pattern matches 10 chars from the A-z0-9 ranges at the start of the string (the ^ only modifies the ([A-z0-9]{10}) part (the first alternative branch), or (|) 1 or more digits at the end of the stirng with (\d+)$ (the $ only modifies the (\d+) branch pattern.
Also note that the A-z is a typo, [A-z] does not only match ASCII letters.
You need to fix it as follows:
var pattern = /^(?:[A-Za-z0-9]{10}|\d+)$/;
or with the i modifier:
var pattern = /^(?:[a-z0-9]{10}|\d+)$/i;
See the regex demo.
Note that grouping is important here: the (?:...|...) makes the anchors apply to each of them appropriately.
Details
^ - start of string
(?: - a non-capturing alternation group:
[A-Za-z0-9]{10} - 10 alphanumeric chars
| - or
\d+ - 1 or more digits
) - end of the grouping construct
$ - end of string
I'm trying to write a regex to match an optional parameter at the end of a path.
I want to cover the first 4 paths but not the last one:
/main/sections/create-new
/main/sections/delete
/main/sections/
/main/sections
/main/sectionsextra
So far I've created this:
/\/main\/sections(\/)([a-zA-z]{1}[a-zA-z\-]{0,48}[a-zA-z]{1})?/g
This only finds the first 3. How can I make it match the first 4 cases?
You may match the string in question up the optional string starting with / with any 1 or or more chars other than / after it up to the end of the string:
\/main\/sections(?:\/[^\/]*)?$
^^^^^^^^^^^^^^
See the regex demo. If you really need to constrain the optional subpart to only consist of just letters and - with the - not allowed at the start/end (with length of 2+ chars), use
/\/main\/sections(?:\/[a-z][a-z-]{0,48}[a-z])?$/i
Or, to also allow 1 char subpart:
/\/main\/sections(?:\/[a-z](?:[a-z-]{0,48}[a-z])?)?$/i
Details
\/main\/sections - a literal substring /main/sections
(?:\/[^\/]*)? - an optional non-capturing group matching 1 or 0 occurrences of:
\/ - a / char
[^\/]* - a negated character class matching any 0+ chars other than /
$ - end of string.
JS demo:
var strs = ['/main/sections/create-new','/main/sections/delete','/main/sections/','/main/sections','/main/sectionsextra'];
var rx = /\/main\/sections(?:\/[^\/]*)?$/;
for (var s of strs) {
console.log(s, "=>", rx.test(s));
}
I need a regex for following values:
Ha2:123hD,Ha2:123hD,Ha2:123hD - correct match - true
Ha2:123hD,Ha2:123hD,Ha2:123hD, - comma on end - false
Ha2:123hD,Ha2:123hD,Ha2:123hD,,Ha2:123hD - double comma- false
,Ha2:123hD,Ha2:123hD,Ha2:123hD - comma at start- false
I am trying the following regex:
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+))/
/(([a-zA-Z0-9]+)(\W)([a-zA-Z0-9]+,)*([a-zA-Z0-9]+)(\W)([a-zA-Z0-9])+$)/
But it is not working.
You could put the comma at the start of the repeating group.
/^[a-zA-Z0-9]+[:][a-zA-Z0-9]+(?:,[a-zA-Z0-9]+[:][a-zA-Z0-9]+)*$/
If you only need to check for these fix strings (As I have to guess from your question), this will work ^(Ha2:123hD,)*Ha2:123hD$
And otherwise, you just can follow this expression and replace the Ha2:123hD with your wildcard expression.
Also check this website:
https://regex101.com/
It explains nicely how the single symbols of a regex work.
To only match comma-seaprated alphanumeric + : string lists you may use
/^[a-zA-Z0-9:]+(?:,[a-zA-Z0-9:]+)*$/
See the regex demo
Explanation:
^ - start of string anchor
[a-zA-Z0-9]+ - a character class matching 1 or more (due to + quantifier)
ASCII letters or digits or :
(?: - start of a non-capturing group....
, - a comma
[a-zA-Z0-9:]+ - a character class matching 1 or more (due to + quantifier) ASCII letters or digits or :
)* - .... 0 or more occurrences (due to the * quantifier)
$ - end of string.
Given: 1999 some text here 1.3i [more]
Needed: some text here
The following regex - replace(/[\d{4} |\d\.*$]/,'') - failed, it just removed the first digit. Any idea why and how to fix it?
var s = "1999 some text here 1.3i [more]"
console.log(s.replace(/[\d{4} |\d\.*$]/,''))
The regex you have removes the first digit only because it matches just 1 char - either a digit, {, 4, }, space, |, ., * or $ (as [...] formed a character class), just once (there is no global modifier).
You may use
/^\d{4}\s+|\s*\d\..*$/g
See the regex demo
Basically, remove the [ and ] that form a character class, add g modifier to perform multiple replacements, and add .* (any char matching pattern) at the end.
Details:
First alternative:
- ^ - start of string
- \d{4} - 4 digits
- \s+ - 1+ whitespaces
Second alternative:
- \s* - 0+ whitespaces
- \d - a digit
- \. - a dot
- .* - any 0+ chars up to...
- $ - the end of the string
var rx = /^\d{4}\s+|\s*\d\..*$/g;
var str = "1999 some text here 1.3i [more]";
console.log(str.replace(rx, ''));