简体   繁体   中英

Regex to match all '&' before first '?'

Basically, I want to do "zipzam&&&?&&&?&&&" -> "zipzam%26%26%26?&&&?&&&". I can do that without regex many different ways, but it'd cleanup things a tad bit if I could do it with regex.

Thanks

Edit: "zip=zam&&&=?&&&?&&&" -> "zip=zam%26%26%26=?&&&?&&&" should make things a little clearer.

Edit: "zip=zam=&=&=&=?&&&?&&&" -> "zip=zam=%26=%26=%26=?&&&?&&&" should make things clearer.

However, theses are just examples. I still want to replace all '&' before the first '?' no matter where the '&' are before the first '?' and no matter if the '&' are consecutive or not.

应该这样做:

"zip=zam=&=&=&=?&&&?&&&".replace(/^[^?]+/, function(match) { return match.replace(/&/g, "%26"); });

you need negative lookbehinds which are tricky to replicate in JS, but fortunately there are ways and means:

var x = "zipzam&&&?&&&?&&&";

x.replace(/(&+)(?=.*?\?)/,function ($1) {for(var i=$1.length, s='';i;i--){s+='%26';} return s;})

commentary: this works because it's not global. The first match is therefore a given, and the trick of replacing all of the matching "&" chars 1:1 with "%26" is achieved with the function loop


edit: a solution for unknown groupings of "&" can be achieved simply (if perhaps a little clunkily) with a little modification. The basic pattern for replacer methods is infinitely flexible.

var x = "zipzam&foo&bar&baz?&&&?&&&";

var f = function ($1,$2)
{
  return $2 + ($2=='' || $2.indexOf('?')>-1 ? '&' : '%26')
}

x.replace(/(.*?)&(?=.*?\?)/g,f)

应该这样做:

^[^?]*&[^?]*\?

In this case regexes are really not the most appropiate things to use. A simple search for the first index of '?' and then replacing each '&' character would be best. However, if you really want a regex then this should do the job.

(?:.*?(&))*?\?

或者我认为:

^[^?]*(&+?)\?

This close enough to what you are after:-

alert("zipzam&&&?&&&?&&&".replace(/^([^&\?]*)(&*)\?/, function(s, p, m)
{
for (var i = 0; i < m.length; i++) p += '%26';
return  p +'?';
}));

Since the OP only wants to match ampersands before the first question mark, slightly modifying Michael Borgwardt's answer gives me this Regex which appears to be appropriate :

^[^?&]*(\&+)\?

Replace all matches with "%26"

This will not match zipzam&&abc?&&&?&&& because the first "?" does not have an ampersand immediately before it.

As far as the javascript part, I can't answer that. But I can give you a regex that will match, separately, each '&' after the first bit of text and before the first '?'

(?<!\?.{0,2})&+?

Which, in human terms, says:

Match as few &'s as possible that are NOT after '?', '?X' or '?XX' where X is any character.

It only matches '&'s, so it won't match anything in the initial bit of text. It's a non-greedy match (+?) so it matches the first & and stops, then matches the second & and stops, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM