i haven't found anything when using google and stack overflow.
I need to match 1-1-1 but not -1-1-1 or 1-1-1- with javascript RegEx.
So it has to start with a number and end with a number and has to be seperated with "-".
I can't figure out, how to do it.
Is it even possible?
Asked
Active
Viewed 308 times
-4

Noah Bitterlich
- 1
- 3
-
Is it a whole string you're trying to validate, or are you trying to extract data from a larger string? In the first case using anchors (`^1-1-1$`) is enough. – Aaron Mar 29 '18 at 18:08
-
yes it is :) can you share what you have tried so far? – Francesco B. Mar 29 '18 at 18:08
-
its part of a text like: "foo 1-1-1 bar" – Noah Bitterlich Mar 29 '18 at 18:09
-
1Grab yourself regex tutorial and https://regex101.com/, and make an attempt yourself first. Then tell us what you came up with. – CBroe Mar 29 '18 at 18:09
-
`^\d+(?:-\d+)+$`? – ctwheels Mar 29 '18 at 18:09
-
I am using regex101.com... – Noah Bitterlich Mar 29 '18 at 18:10
-
Then you should have a few attempts to present already, no? – CBroe Mar 29 '18 at 18:10
-
Ive tried \d(-\d)+ but this also matches parts of -1-1-1. and i've tried the same with ^$ but still didn't work – Noah Bitterlich Mar 29 '18 at 18:12
-
Well if you don't like a `-` before or after, then demand that something else be there ...? A digit would still be a match, so you want to match either start of subject, or a non digit non dash/minus, before your first digit. Vice versa on the other end ... – CBroe Mar 29 '18 at 18:13
-
but what should i put there? – Noah Bitterlich Mar 29 '18 at 18:14
-
Well for example a negated character class. – CBroe Mar 29 '18 at 18:15
-
and how exactly? – Noah Bitterlich Mar 29 '18 at 18:17
-
https://jsfiddle.net/yvog9wyk/ - that should be pretty close already? If you want the last few single-digit cases included, switch the + out for a *. Now the white space (or other characters that might get caught too, if `foo1-1-1 bar` was valid as well) gets captured here as well, because of the grouping brackets around the alternatives at the beginning and end. If it can only be spaces (example given wasn't clear), you might just trim the result, otherwise it might need a bit more tweaking. – CBroe Mar 29 '18 at 18:24
1 Answers
1
Unfortunately, JavaScript regex doesn't have a look-behind (see javascript regex - look behind alternative?), so to exclude a preceding -
, the regex will have to match on the preceding character too (as long as it's not a -
).
Since there might not be a preceding character (input starts with 1
), you have to also match on beginning of input (^
).
So, this regex will do it: (?:[^-]|^)(1(?:-1)+)(?!-)
See regex101.com.
Whether it should match a standalone 1
, or only on 1-1
(and longer), is up to you. The regex above will not match standalone 1
. Change +
to *
to change that.
I also added capturing of the actual text you wanted to match, i.e. without the leading character. You can remove the extra ()
around 1(?:-1)+
if that's not needed.

Andreas
- 154,647
- 11
- 152
- 247
-
-
Lookbehinds are gaining support in ECMA. Chrome already supports them: https://github.com/tc39/proposal-regexp-lookbehind – ctwheels Mar 29 '18 at 18:24
-