Javascript 条件正则表达式 if-then-else
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32852857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript conditional regular expression if-then-else
提问by user974061
I'm trying to limit the entries to a specific format.
我试图将条目限制为特定格式。
If the entry has 5500 or 5100 such as 01\01-5500-000-00 then I want to have this:
如果条目有 5500 或 5100,例如 01\01-5500-000-00 那么我想要这个:
^[0-9]{2,}\[0-9]{2}\-[0-9]{4}\-[0-9]{3}\-$
But if the entry has anything other than 5500 or 5100 I want to have this:
但是,如果条目有 5500 或 5100 以外的任何内容,我想要这个:
^[0-9]{2,}\[0-9]{2}\-[0-9]{4}\-[0-9]{3}\-[0-9]{2}$
How can this be accomplished with the if then else idea?
如何通过 if then else 想法实现这一点?
回答by Wiktor Stribi?ew
Conditional regex syntax is not supported by JavaScript regex engine, but it can be worked around with a non-capturing group containing 2 alternatives:
JavaScript 正则表达式引擎不支持条件正则表达式语法,但可以使用包含 2 个替代方案的非捕获组来解决它:
One with the positive look-ahead and
The second with the reversed, negative look-ahead.
一种具有积极前瞻性和
第二个是反向的、负面的前瞻。
This regex meets your criteria and is JavaScript compatible:
此正则表达式符合您的条件并且与 JavaScript 兼容:
^(?:(?=.*\b5[15]00\b)[0-9]{2,}\[0-9]{2}-[0-9]{4}-[0-9]{3}-|(?!.*\b5[15]00\b)[0-9]{2,}\[0-9]{2}-[0-9]{4}-[0-9]{3}-[0-9]{2})$
See regex demo
Let me break it down:
让我分解一下:
^
- Start of string(?:
(?=.*\b5[15]00\b)[0-9]{2,}\\[0-9]{2}-[0-9]{4}-[0-9]{3}-
- First alternative with the(?=.*\b5[15]00\b)
look-ahead that requires a whole word5500
or5100
inside the string, and the first pattern you have|
- alternation operator(?!.*\b5[15]00\b)[0-9]{2,}\\[0-9]{2}-[0-9]{4}-[0-9]{3}-[0-9]{2})
- Second alternative that is prepended with the(?!.*\b5[15]00\b)
negative look-ahead that makes sure there is no5100
or5500
inside the string, and only then matches your second pattern.
$
- end of string.
^
- 字符串的开始(?:
(?=.*\b5[15]00\b)[0-9]{2,}\\[0-9]{2}-[0-9]{4}-[0-9]{3}-
-(?=.*\b5[15]00\b)
需要整个单词5500
或5100
字符串内部的前瞻的第一个替代方案,以及您拥有的第一个模式|
- 交替运算符(?!.*\b5[15]00\b)[0-9]{2,}\\[0-9]{2}-[0-9]{4}-[0-9]{3}-[0-9]{2})
- 第二个替代方案,它带有(?!.*\b5[15]00\b)
否定前瞻,确保没有5100
或5500
在字符串内,然后才匹配您的第二个模式。
$
- 字符串的结尾。
回答by Eder
Use conditionals, Eg:
使用条件,例如:
(?(?=regex)then|else): [0-9]{2,}\\[0-9]{2}\-(?(?=5[15]00)[0-9]{4}\-[0-9]{3}\-|[0-9]{4}\-[0-9]{3}\-[0-9]{2})
(?(?=regex)then|else):[0-9]{2,}\\[0-9]{2}\-(?(?=5[15]00)[0-9]{4}\-[0-9]{3}\-|[0-9]{4}\-[0-9]{3}\-[0-9]{2})
Regex conditionals example: [email protected]
正则表达式条件示例:[email protected]
In case that you're using a regex enginethat is not PCRE based, you can be able to mimic the functionality by doing the following:
如果您使用的正则表达式引擎不是基于 PCRE 的,您可以通过执行以下操作来模拟该功能:
((?=positive-regex-statement)then|(?!negavite-regex-statement)then)
Eg.
例如。
^[0-9]{2,}\[0-9]{2}\-((?=5[15]00)[0-9]{4}\-[0-9]{3}\-|[0-9]{4}\-|(?!5[15]00)[0-9]{4}\-[0-9]{3}\-[0-9]{2})$
Mimic regex conditionals example: [email protected]
模拟正则表达式条件示例:[email protected]
回答by Mariano
Replace this part of the pattern [0-9]{4}
with the literal values for 5100/5500:
用[0-9]{4}
5100/5500 的文字值替换模式的这一部分:
/^\d{2,}\\d{2}-(?:5[15]00-\d{3}-|(?!5[15]00)\d{4}-\d{3}-\d{2})$/
[0-9]
is the same thing as\d
in JavaScript- For the else part, use the negative lookahead
(?!5[15]00)\d{4}
to guarantee it's not 5100/5500.
[0-9]
与\d
JavaScript 中的相同- 对于 else 部分,使用负前瞻
(?!5[15]00)\d{4}
来保证它不是 5100/5500。