Javascript 正则表达式:匹配任何东西直到某物(如果存在)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8584697/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:35:21  来源:igfitidea点击:

Javascript regular expression: match anything up until something (if there it exists)

javascriptregexnegative-lookahead

提问by undefinederror

Hi I am new to regular expression and this may be a very easy question (hopefully).

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

I am trying to use one solution for 3 kind of string

我正在尝试对 3 种字符串使用一种解决方案

  • "45%", expected result: "45"
  • "45", expected result: "45"
  • "", expected result: ""
  • “45%”,预期结果:“45”
  • “45”,预期结果:“45”
  • ““, 预期结果: ””

What I am trying (let the string be str):

我在尝试什么(让字符串为 str ):

str.match(/(.*)(?!%*)/i)[1]

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

让我们/\d*/暂时忘记在这种特定情况下我只匹配数字,所以会这样做。我试图理解一个一般规则,以便我可以随时应用它。

Anybody would be so kind to help me out?

有人会这么好心帮我吗?

回答by kennytm

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 positivelook-ahead instead of negative.

编辑:如果需要解析直到</a>,那么您可以解析一个序列 pf 字符,然后是</a>,然后丢弃</a>,这意味着您应该使用积极的前瞻而不是消极的。

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

This means: match zero-or-more character lazily, until reaching a </a>or end of string.

这意味着:延迟匹配零个或多个字符,直到到达</a>字符串的结尾或结尾。

Note that *?is a single operator, (.*)?is not the same as .*?.

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

(And don't parse HTML with a single regex, as usual.)

(并且不要像往常一样使用单个 regex 解析 HTML。)

回答by Alan Moore

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:

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

/[^%]*/

But for a multi-character sentinel like </a>, you have to use the lookahead approach:

但是对于像 那样的多字符哨兵</a>,您必须使用前瞻方法:

/(?:(?!</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".

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

回答by bobbymcr

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

使用精确搜索字符串的最简单方法是跳过正则表达式并只使用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);
}

回答by jermel

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 %

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

回答by Richard Logwood

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

要匹配 45、45% 和任意数量的任意长度,请使用它(182%、18242 等)

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.

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

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)$"

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

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