Javascript正则表达式:匹配任何内容直到某些内容(如果存在)
我是正则表达式的新手,这可能是一个非常简单的问题(希望如此)。
我正在尝试对 3 种字符串
- “45%”使用一种解决方案,预期结果:“45”
- “45”,预期结果:“45”
- “”,预期结果:“”
我正在尝试的内容(让字符串为str):
str.match(/(.*)(?!%*)/i)[1]
这在我的脑海中听起来像是“匹配任何实例,直到找到'%',否则就匹配任何东西”
在firebug的头脑中,这听起来更像是“只匹配任何东西并完全忽略”消极的向前看”。另外,为了让它变得懒惰 - (.*)?
- 似乎没有帮助。
让我们暂时忘记,在这种特定情况下,我只匹配数字,因此 /\d*/
就可以了。我试图理解一般规则,以便我可以随时应用它。
有人会好心帮助我吗?
I am new to regular expression and this may be a very easy question (hopefully).
I am trying to use one solution for 3 kinds of string
- "45%", expected result: "45"
- "45", expected result: "45"
- "", expected result: ""
What I am trying (let the string be str):
str.match(/(.*)(?!%*)/i)[1]
This is in my head would sound like "match any instance of anything up until '%' if it is found, or else just match anything"
In firebug's head, it seems to sound more like "just match anything and completely disregard the negative lookahead". Also to make it lazy - (.*)?
- doesn't seem to help.
Let's forget for a second that in this specific situation I am only matching numbers, so a /\d*/
would do. I am trying to understand a general rule so that I can apply it whenever.
Anybody would be so kind to help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更简单的怎么样?
这意味着,匹配零个或多个字符,这不是
%
。编辑:如果需要解析直到
,那么您可以解析一个pf字符序列,后跟
,然后丢弃
,这意味着您应该使用正向前瞻而不是负向。
这意味着:延迟匹配零个或多个字符,直到到达
或字符串末尾。
请注意,
*?
是单个运算符,(.*)?
与.*?
不同。(并且像往常一样,不要使用单个正则表达式解析 HTML。)
How about the simpler
Which means, match zero-or-more character, which is not a
%
.Edit: If need to parse until
</a>
, then you could parse a sequence pf characters, followed by</a>
, then then discard the</a>
, which means you should use positive look-ahead instead of negative.This means: match zero-or-more character lazily, until reaching a
</a>
or end of string.Note that
*?
is a single operator,(.*)?
is not the same as.*?
.(And don't parse HTML with a single regex, as usual.)
我认为这就是您要寻找的:
.
匹配任何字符,但仅在否定前瞻(?!%)
之后,确认该字符不是<代码>%。请注意,当哨兵是单个字符(如%
)时,您可以使用否定字符类,例如:但对于多字符哨兵(如
) >,你必须使用前瞻方法:
这实际上是说“一次匹配零个或多个字符,但如果下一个字符结果是序列的开头
或
,停止而不消耗它”。
I think this is what you're looking for:
The
.
matches any character, but only after the negative lookahead,(?!%)
, confirms that the character is not%
. Note that when the sentinel is a single character like%
, you can use a negated character class instead, for example:But for a multi-character sentinel like
</a>
, you have to use the lookahead approach:This is actually saying "Match zero or more characters one at a time, but if the next character turns out to be the beginning of the sequence
</a>
or</A>
, stop without consuming it".使用精确搜索字符串的最简单方法是跳过正则表达式并仅使用
indexOf
,例如:The easiest way with an exact search string is to skip regular expressions and just use
indexOf
, e.g.:我只是按照你所说的那样写的:
这将匹配任何不带 '%' 的字符串或包含 % 的字符串的第一部分。
您必须获得第一组或第二组的结果。
编辑:这正是您想要的下面
因此正则表达式读取匹配任何不包含 % 的字符串包含 %,或(否则匹配)第一个 % 之前的所有字符
I just wrote it exactly how you said it:
This will match any string without a '%' or the first part of a string that contains a %.
You have to get the result from the 1st or 2nd group.
EDIT: This is exactly what you want below
so the regex reads match any string that doesnt contain %, OR (otherwise match) all of the characters before the first %
要匹配 45、45% 和任何长度的任何数字,请使用此(182%、18242 等),
如果需要匹配空字符串,请将其包含为 ^$,注意 match(". ..")[1] 对于空字符串将是未定义的,因此您需要测试匹配,然后检查 [0] 或查看 [1] 是否未定义。
如果您需要精确匹配两个数字,请使用 {2,2} 将表达式锚定在开始行和结束行字符之间:“^(exp)$”
to match 45, 45%, and any number of any length use this (182%, 18242, etc)
if you need to match the empty string also include it as ^$, note match("...")[1] will be undefined for the empty string, so you will need to test for match and then check [0] or see if [1] is undefined.
if you need to match exactly two digits use {2,2} anchor the expression between begin and end line characters: "^(exp)$"