开发者

Help with a regex

I've got the following sequence I'm attempting to detect...#hl=b&xhr=a where b is equal to anything and a is equal to anything.

I've got the following.. but it doesn't appear to be working... (#hl=.+&xhr=) Does anyone know why?

I'm using jav开发者_运维问答ascript and values a and b are letters of the alphabet.


(#hl=.+&xhr=.+), you missed the second .+. Depending on your regex engine, you should also see their escaping rules, often the braces or the + have to be escaped. If you just want to match a whole string, the braces are not needed anyway, btw.


You'll need to be more specific to get a better answer:

  • what programming language are you using RegEx in?
  • what values can a and b have? Anything implies that newlines are included, which . doesn't match
  • do you want to get the values of a and b?

Now that that's all been said, lets move onto a regex with some assumptions:

/#h1=(.+)&xhr=(.+)/

This will match a string #h1=a&xhr=b and select the a and b values from the string. It will be greedy, so if there are key-value pairs in the pseudo-URL (I assume it's a url encoded string as a hashtag) they will be matched in b.

#h1=a&xhr=b&foo=bar

the second selection will match b&foo=bar.

The regex also assumes #h1= comes before &xhr=.


Assuming #, & and = are special characters, how about this regular expression:

#h1=([^#&=]+)&xhr=([^#&=]+)

Are you sure your key/value pairs (?) are always in this order without anything in between?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜