18

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.

4
  • 2
    if you're parsing query strings, there're better ways of doing it. What language is the regex supposed to be implemented in anyway? Commented Nov 15, 2011 at 18:40
  • I am doing it in jquery, if that doesn't work I will use it in shell with grep Commented Nov 15, 2011 at 18:42
  • 2
    You are doing in JavaScript then. jQuery is a library whereas JavaScript is a language. It's like asking what language you're speaking and you reply "Library of Congress". Commented Nov 15, 2011 at 18:44
  • If you're parsing the query string you could see stackoverflow.com/questions/2090551/… or my answer to a related question Commented Nov 15, 2011 at 18:48

3 Answers 3

20

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/

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0
/=([^&]*)&/

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

1 Comment

While this is correct, the usual way of writing it is .*?& by using lazy quantifiers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.