Javascript正则表达式:匹配任何内容直到某些内容(如果存在)

发布于 2024-12-22 09:06:50 字数 465 浏览 4 评论 0原文

我是正则表达式的新手,这可能是一个非常简单的问题(希望如此)。

我正在尝试对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云雾 2024-12-29 09:06:50

更简单的怎么样?

str.match(/[^%]*/i)[0]

这意味着,匹配零个或多个字符,这不是 %


编辑:如果需要解析直到,那么您可以解析一个pf字符序列,后跟 ,然后丢弃,这意味着您应该使用正向前瞻而不是负向。

str.match(/.*?(?=<\/a>|$)/i)[0]

这意味着:延迟匹配零个或多个字符,直到到达 或字符串末尾。

请注意,*? 是单个运算符,(.*)?.*? 不同。

(并且像往常一样,不要使用单个正则表达式解析 HTML。)

How about the simpler

str.match(/[^%]*/i)[0]

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.

str.match(/.*?(?=<\/a>|$)/i)[0]

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.)

記憶穿過時間隧道 2024-12-29 09:06:50

我认为这就是您要寻找的:

/(?:(?!%).)*/

. 匹配任何字符,但仅在否定前瞻 (?!%) 之后,确认该字符不是<代码>%。请注意,当哨兵是单个字符(如 %)时,您可以使用否定字符类,例如:

/[^%]*/

但对于多字符哨兵(如 ) >,你必须使用前瞻方法:

/(?:(?!</a>).)*/i

这实际上是说“一次匹配零个或多个字符,但如果下一个字符结果是序列的开头,停止而不消耗它”。

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:

/(?:(?!</a>).)*/i

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".

雨后彩虹 2024-12-29 09:06:50

使用精确搜索字符串的最简单方法是跳过正则表达式并仅使用indexOf,例如:

// String to be searched
var s = "Here is a <a>link</a>."

// String to find
var searchString = "</a>";

// Final match
var matched = "";

var c = s.indexOf(searchString);
if (c >= 0)
{
    // Returns the portion not including the search string;
    // in this example, "Here is a <a>link". If you want the
    // search string included, add the length of the search
    // string to c.
    matched = s.substring(c);
}

The easiest way with an exact search string is to skip regular expressions and just use indexOf, e.g.:

// String to be searched
var s = "Here is a <a>link</a>."

// String to find
var searchString = "</a>";

// Final match
var matched = "";

var c = s.indexOf(searchString);
if (c >= 0)
{
    // Returns the portion not including the search string;
    // in this example, "Here is a <a>link". If you want the
    // search string included, add the length of the search
    // string to c.
    matched = s.substring(c);
}
撩人痒 2024-12-29 09:06:50

我只是按照你所说的那样写的:

str.match(/(^[^%]*$)|^([^%]*)%.*/i)

这将匹配任何不带 '%' 的字符串或包含 % 的字符串的第一部分。
您必须获得第一组或第二组的结果。

编辑:这正是您想要的下面

str.match(/(?:^[^%]*$)|^(?:[^%]*)(?=%)/)
  • ?: 删除所有分组
  • ?= 是一个前瞻,查看字符串是否包含 %
  • 和 [^%] 匹配任何不是 % 的字符,

因此正则表达式读取匹配任何不包含 % 的字符串包含 %,或(否则匹配)第一个 % 之前的所有字符

I just wrote it exactly how you said it:

str.match(/(^[^%]*$)|^([^%]*)%.*/i)

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

str.match(/(?:^[^%]*$)|^(?:[^%]*)(?=%)/)
  • The ?: removes all grouping
  • The ?= is a lookahead to see if the string contains %
  • and [^%] matches any character that is not a %

so the regex reads match any string that doesnt contain %, OR (otherwise match) all of the characters before the first %

归途 2024-12-29 09:06:50

要匹配 45、45% 和任何长度的任何数字,请使用此(182%、18242 等),

str.match(/([0-9]+)([%]?)/)[1];

如果需要匹配空字符串,请将其包含为 ^$,注意 match(". ..")[1] 对于空字符串将是未定义的,因此您需要测试匹配,然后检查 [0] 或查看 [1] 是否未定义。

str.match(/([0-9]+)([%]?)|^$/)

如果您需要精确匹配两个数字,请使用 {2,2} 将表达式锚定在开始行和结束行字符之间:“^(exp)$

str.match(/^([0-9]{2,2})([%]?)$/)[1];

to match 45, 45%, and any number of any length use this (182%, 18242, etc)

str.match(/([0-9]+)([%]?)/)[1];

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.

str.match(/([0-9]+)([%]?)|^$/)

if you need to match exactly two digits use {2,2} anchor the expression between begin and end line characters: "^(exp)$"

str.match(/^([0-9]{2,2})([%]?)$/)[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文