javascript - Regex to match all '&' before first '?' - Stack Overflow

Basically, I want to do "zipzam&&&?&&&?&&&" -> "z

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.

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.

Share Improve this question edited Apr 3, 2009 at 22:19 Chad Birch 74.7k23 gold badges155 silver badges150 bronze badges asked Jan 26, 2009 at 13:03 Shadow2531Shadow2531 12.2k5 gold badges38 silver badges51 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 4

This should do it:

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

mentary: 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)

This should do it:

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

Or this one, I think:

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742421361a4440744.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信