I just wanted to ask for an example of a string that would match this regular expression for JS:
/\/[a-z]{2,6}\/(\([0-9]+\)?$/
But this bit confuses me: /(\([0-9]+\)?$/
If I could get an example of a string using this regular expression and a brief explanation, that would be enough to clear it up for me.
Thanks!
EDITED: Sorry for the trouble, I missed a parentheses, however I just want to clear up, a string that would match would be such as:
/ab/(12345) or /abcdef/(1) etc right?
Here is a visual representation of the #Paul Roub explanation:
EDITED with last pattern
\/[a-z]{2,6}\/\([0-9]+\)$
Debuggex Demo
This example is OK : /abcdef/(12345)
\/
A forward slash (escaped), followed by
[a-z]{2,6}
between two and six lowercase English letters, followed by
\/
another slash
([0-9]+)?
the inside part - one or more digits. ? means "zero or one", so we're looking for a string of digits, or nothing. The parentheses would let this number be captured as a group for later processing
$
and the end of the string.
Things that would match:
/ab/0
/ab/
/acdefg/12345
things that would not match
/a/0
/abcdefgh/12345
/ab/0x
That regexp is malformed.
Opening bracket is escaped, but closing one not
Opening paren is not escaped but closing one is
I think the intended regexp was:
/\/[a-z]{2,6}\/([0-9]+)?$/
This would match:
/ab/1
/ab/
/abc/123
Wouldn't match:
/a/1
ab/1
/abcdefg/123
Cheers.
Related
Happy Saturday,
I'm wondering if Stackoverflow's users could give me a clue about one specific Regex..
(^visite\d+)(?!\D)
The above regex works well..
It says that :
visite12345 --> is a good anwser (the string does match)
visite1a --> is not a good anwser (the string doesn't match)
However for:
visite12345a --> It doesn't work.
Indeed, the output is visite1234, whereas I'd like to get the same answer that for visite1a (string doesn't match)...
I use http://regexr.com/ to test my regexp.
Do you have any idea how to so?
Thank you very much.
The regex (^visite\d+)(?!\D) matches visite at the start of the string, followed with one or more digits that should not be followed with a non-digit.
The "issue" is that the engine can backtrack within \d+ pattern and it can match 2 digits if the third is not followed with a nondigit.
The best way to solve it is to check the actual requirements and adjust the pattern.
If the digits are the last characters in the string you just should replace the lookahead with the $ anchor.
A generic solution for this is making the subpattern atomic with a capturing group inside a positive lookahead and a backreference, and make sure the lookahead is changed to something like (?![a-zA-Z]) - fail if there is a letter):
/^visite(?=(\d+))\1(?![a-z])/i
See the regex demo
Or if a word boundary should follow the digits (i.e. digits should be followed with a letter, digit or an underscore), use \b instead of the lookahead:
/^visite\d+\b/
See another demo
While trying to submit a form a javascript regex validation always proves to be false for a string.
Regex:- ^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[\\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
I have tried following strings against it
abc.jpg,
abc:.jpg,
a:.jpg,
a:asdas.jpg,
What string could possible match this regex ?
This regex won't match against anything because of that $? in the middle of the string.
Apparently using the optional modifier ? on the end string symbol $ is not correct (if you paste it on https://regex101.com/ it will give you an error indeed). If the javascript parser ignores the error and keeps the regex as it is this still means you are going to match an end string in the middle of a string which is supposed to continue.
Unescaped it was supposed to match a \$ (dollar symbol) but as it is written it won't work.
If you want your string to be accepted at any cost you can probably use Firebug or a similar developer tool and edit the string inside the javascript code (this, assuming there's no server side check too and assuming it's not wrong aswell). If you ignore the $? then a matching string will be \\\\w\\\\ww.jpg (but since the . is unescaped even \\\\w\\\\ww%jpg is a match)
Of course, I wrote this answer assuming the escaping is indeed the one you showed in the question. If you need to find a matching pattern for the correctly escaped one ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(\.jpeg|\.JPEG|\.jpg|\.JPG)$ then you can use this tool to find one http://fent.github.io/randexp.js/ (though it will find weird matches). A matching pattern is c:\zz.jpg
If you are just looking for a regular expression to match what you got there, go ahead and test this out:
(\w+:?\w*\.[jpe?gJPE?G]+,)
That should match exactly what you are looking for. Remove the optional comma at the end if you feel like it, of course.
If you remove escape level, the actual regex is
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
After ^start the first pipe (([a-zA-Z]:)|(\\{2}\w+)\$?) which matches an alpha followed by a colon or two backslashes followed by one or more word characters, followed by an optional literal $. There is some needless parenthesis used inside.
The second part (\\(\w[\w].*))+ matches a backslash, followed by two word characters \w[\w] which looks weird because it's equivalent to \w\w (don't need a character class for second \w). Followed by any amount of any character. This whole thing one or more times.
In the last part (.jpeg|.JPEG|.jpg|.JPG) one probably forgot to escape the dot for matching a literal. \. should be used. This part can be reduced to \.(JPE?G|jpe?g).
It would match something like
A:\12anything.JPEG
\\1$\anything.jpg
Play with it at regex101. A better readable could be
^([a-zA-Z]:|\\{2}\w+\$?)(\\\w{2}.*)+\.(jpe?g|JPE?G)$
Also read the explanation on regex101 to understand any pattern, it's helpful!
I am trying to build a RegEx in Javascript that does not match if a certain string appear. So I want to match this curly bracket { but only if in front of it is not the string else.
I am trying to do this ^ *[^else]* *{.*$ but in fact this doe not match if any character in elsestring appear, for example this does not match also this:
erai {
I want to match all the cases when { appear despite of this case else {.
Please could you help me. Here is my DEMO
You can use a negative lookahead. This is supported by JavaScript:
(?!\s*else).+ *({).*$|
DEMO
JavaScript RegEx doesn't support ifs but we can use a trick for it to work:
(?!RegExp)
That's the first part, if RegExp (which is a regex) doesn't appear, then we do the code after that:
.+ *({).*$
That's the RegEx we run. Broken does, it is:
.+ Match anything
* Until 0 - unlimited spaces
({) Capture the {
.*$ Match anything till the end
Now this won't work unless we add a | at the end, or an OR. This will trick it into working like an if statement
Debuggex Demo
What you are looking for is called negative lookbehind.
I need to match incorrect backslashes in a text. The following text is an example:
\.br\ Random Words \.br\\1 Testing\.br\2\ Check
So the \.br\ are correct, however the backslashes in \1 and 2\ are not.
So I attempted a regular expression to match any \ which is not followed by a .br but that failed because it would match the closing \ in \.br\
I then looked up a few similar questions on stackoverflow and most of them stated that a series of lookaheads can be used as an 'and' operator and so I tried this:
/(?!\\\.br)\\(?!\.br\\)/
What I attempted to do, was match any backslash that was neither precedeed by a \.br nor followed by a .br\ but it didn't seem to work.
Any help would be appreciated. I hope I haven't missed out any details in the question.
Thanks,
Sid
Close. (?!PAT) means "not followed by PAT". You want "not preceded by PAT".
(?<!\\\.br)\\(?!\.br\\)
The following will be a bit faster:
\\(?<!\\\.br\\)(?!\.br\\)
I would use perl, and with a \G anchor and a \K meta character (and some atomic/possessive parts to improve efficiency):
\G(?>\\\.br\\|[^\\]++)*+\K\\
It should be faster than using lookarounds, since there's no duplication of matches (going over the same substring more than once, which is what lookarounds do).
regex101 demo.
Matches completed with 24 and 21 steps respectively (as opposed to using lookarounds using 36 and 22 steps, plus 4 failing steps).
(?:\\(?!\.br)\\)+(\S+)
The regex above will capture those characters inside backslashes that are not .br.
*Please note that the number 2 in \.br\2\ will not be captured as .br\ is correctly typed.
Like the title says, I have a (faulty) Regex in JavaScript, that should check for a "2" character (in this case) surrounded by slashes. So if the URL was http://localhost/page/2/ the Regex would pass.
In my case I have something like http://localhost/?page=2 and the Regex still passes.
I'm not sure why. Could anyone tell me what's wrong with it?
/^(.*?)\b2\b(.*?$)/
(I'm going to tell you, I didn't write this code and I have no idea how it works, cause I'm really bad with Regex)
Seems too simple but shouldn't this work?:
/\/2\//
http://jsfiddle.net/QHac8/1/
As it's javascript you have to escape the forward slashes as they are the delimiters for a regex string.
or if you want to match any number:
/\/\d+\//
You don't check for a digit surrounded by slashes. The slashes you see are only your regex delimiters. You check for a 2 with a word boundary \b on each side. This is true for /2/ but also for =2
If you want to allow only a 2 surrounded by slashes try this
/^(.*?)\/2\/(.*?)$/
^ means match from the start of the string
$ match till the end of the string
(.*?) those parts are matching everything before and after your 2 and those parts are stored in capturing groups.
If you don't need those parts, then Richard D is right and the regex /\/2\// is fine for you.