javascript 正则表达式查找所有以 = 开头并以 & 结尾的字符串

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

regex find all the strings preceded by = and ending in &

javascriptregex

提问by anon

I need to find in a large body of text all the strings that are between = and & symbols. I don't want the result strings to contain = and &, only whats between them.

我需要在大量文本中找到 = 和 & 符号之间的所有字符串。我不希望结果字符串包含 = 和 &,只有它们之间的内容。

回答by Regexident

If your regex engine supports lookbehinds/lookaheads:

如果您的正则表达式引擎支持lookbehinds/lookaheads:

(?<==).*?(?=&)

Otherwise use this:

否则使用这个:

=(.*?)&

and catch capture group 1.

并捕获捕获组 1。

If your regex engine does not support non-greedy matching replace the .*?with [^&]*.

如果您正则表达式引擎不支持非贪婪的匹配替换.*?使用[^&]*



But as zzzzBov mentioned in a comment, if you're parsing GETURL prefixes there are usually better native methods for parsing GETarguments.

但正如 zzzzBov 在评论中提到的,如果您正在解析GETURL 前缀,通常有更好的本地方法来解析GET参数。

In PHP for example there would be:

例如,在 PHP 中会有:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
?>

(As found on php.net.)

(如在php.net 上找到的那样。)

Edit:Appears you're using Javascript.

编辑:看来您正在使用 Javascript。

Javascript solution for parsing query string into object:

用于将查询字符串解析为对象的 Javascript 解决方案:

var queryString = {};
anchor.href.replace(
    new RegExp("([^?=&]+)(=([^&]*))?", "g"),
    function(
/(?<==).*?(?=&)/
, , , ) { queryString[] = ; } );

Source: http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/

资料来源:http: //stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/

回答by FailedDev

Assuming your regex engine supports lookaheads.

假设您的正则表达式引擎支持前瞻。

var myregexp = /=(.*?)(?=&)/g;
var match = myregexp.exec(subject);
while (match != null) {
    for (var i = 0; i < match.length; i++) {
        // matched text: match[i]
    }
    match = myregexp.exec(subject);
}

Edit :

编辑 :

Javascript doesn't support lookbehind so :

Javascript 不支持后视,所以:

"
=       # Match the character “=” literally
(       # Match the regular expression below and capture its match into backreference number 1
   .       # Match any single character that is not a line break character
      *?      # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?=     # Assert that the regex below can be matched, starting at this position (positive lookahead)
   &       # Match the character “&” literally
)
"

this is what you should use.

这是你应该使用的。

Explanation :

解释 :

/=([^&]*)&/

回答by Kevin

##代码##

You'll of course need to adapt the syntax and what to do with it.

您当然需要调整语法以及如何处理它。