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.
问题:
回答1:
If your regex engine supports lookbehinds/lookaheads:
(?<==).*?(?=&)
Otherwise use this:
=(.*?)&
and catch capture group 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 GET
URL prefixes there are usually better native methods for parsing GET
arguments.
In PHP for example there would be:
<?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.)
Edit: Appears you're using Javascript.
Javascript solution for parsing query string into object:
var queryString = {};
anchor.href.replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) { queryString[$1] = $3; }
);
Source: http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/
回答2:
Assuming your regex engine supports lookaheads.
/(?<==).*?(?=&)/
Edit :
Javascript doesn't support lookbehind so :
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);
}
this is what you should use.
Explanation :
"
= # 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
)
"
回答3:
/=([^&]*)&/
You'll of course need to adapt the syntax and what to do with it.