PDA

View Full Version : RegExp question


Algorithm
10-22-2002, 11:04 PM
Is it possible to specify a repeating pattern of several characters in regexp format?

jkd
10-22-2002, 11:08 PM
(?:mypattern){3}

Will match "mypatternmypatternmypattern"

beetle
10-22-2002, 11:20 PM
Mind if I but in here with my own Q?

Hey jkd, tell me exactly what the ?: does I mean, how is what you did different from

(mypattern){3}

I'm just a tad confused...

Algorightm, if you want a pattern to exactly repeat that isn't literal, like what is demoed above...use backreferences...

For example...

(\w\d){3}

will match "a2v8s3" or "z6i5r1" or "b4p7n2"

But, the following pattern

(\w\d)\\1{2}

will match only "a2a2a2" or "z6z6z6" or "b4b4b4"

got that?

Algorithm
10-22-2002, 11:22 PM
Yeah, thanks.

jkd
10-22-2002, 11:27 PM
() remembers what it matches, and the results show up in match data results, such as returned by match() or exec().

(?:) is non-capturing grouping. It groups without remembering, which allows for more complex RegExp without polluting the match results.

You should look into a few of the new RegExp grouping methods found in the JS 1.5 docs, but seem to work in IE6 as well.

beetle
10-22-2002, 11:52 PM
Got it. Thanks.

whammy
10-23-2002, 01:49 AM
Aha! Does that work for other languages like VBScript as well (I'm going to go test it right now, lol)?

If that's the case I can shorten my code by about 2 characters...

Oh wait... never mind it's probably the same number of chars either way. Darn it. But that's a good thing to know. :D

whammy
10-23-2002, 01:54 AM
It works. Yay. Thanks buddy... and only two extra bytes in my regex functions. I can live with that. :D

Man I love little tidbits of knowledge that you pick up here and there... that one is awesome. You are the man, jkd.

jkd
10-23-2002, 02:15 AM
Take a look here if you like (?:):
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/regexp.html#1193136

whammy
10-23-2002, 02:17 AM
I shall bookmark that link and peruse it at my earliest possible convenience. :D