开发者

Violating RegExp: Every string that is smaller or equal "001700"

i have a unique challenge.

i want to create a google analytics filter for a custom variable that only returns a value if the given string is smaller or equal than '001700'. yeah, i know that a string can't be smaller, still i need to find a way to make this work.

oh, and if you ask: no there is no way to convert that string to a number (according to my knowledge - via a google analytics filter - and that is what i have to work with in this case).

so basically, i have

000000
000001
000002
000003
...
...
999998
99999开发者_C百科9

and i need a regular expression that matches

001700
001699
001698
...
...
000001
000000

but does not match

001701
001702
...
...
999998
999999

sub question a) is it possible? (as i have learned, everything is possible with regExp if you are clever and/or masochistic enough)

sub question b) how to do it?

thx very much


You can do:

^00(1700|1[0-6][0-9]{2}|0[0-9]{3})$

See it


yes you can do

see this article

Eg:

alert('your numericle string'.replace(/\d+/g, function(match) {
    return parseInt(match,10) <= 17000 ? '*' : match;
}));

JavaScript calls our function, passing the match into our match argument. Then, we return either the asterisk (if the number matched is under 17000) or the match itself (i.e. no match should take place).


Can be done with RegEx:

/00(1([0-6][0-9]{2}|700)|0[0-9]{3})/

Explanation:

00 followed by

  • 1 followed by 0 to 6 and any 2 numbers = 1000 - 1699

or

  • 1700

or

  • 0 followed by any 3 numbers = 0000 - 0999
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜