View Full Version : RegExp match extraction
04-12-2005, 03:53 PM
I have a string containing an pseudo-array serialized in that form '@key1:value1@key2:value2'
I want to extract all keys but I can't get rid of the opening '@' and trailing ':' out of each key... :(
Is there some wise developper able to point out the ugly mistake in my code:
var text = "@key1:value1@key2:value2";
var keys = text.match(new RegExp("@([^@:]*):", 'g');
Thanks in advance.
PS: as a workaround, I sliced(1, -1) the keys, but I'm looking for a more general solution...
04-12-2005, 05:58 PM
If you're doing a global search you will be using the entire match and not just the captured part of it. There's no easy way of getting rid of the '@', but the easy way of making sure the ':' is not included is to write a regex that does not match ':' at the end, like this /@[^:]+/g.
Regex functionality can be tricked into doing lots of stuff if you want something more than a list of keys, though. Look at this, for example:
04-12-2005, 07:05 PM
Indeed, this is very interesting and you had the right guess on my intentions :)
In fact I obviously want to build an collection, as an array in my case. By the way, why would I prefer an object as you do? I'm pretty new to JSing, and I've seen that many times, but couldn't figure out why by myself.
Another question: it looks like there's an unused 'm' parameter in the callback sent to replace (I didn't know this was possible). Is it the typical mandatory prototype for the function?
Anyway, thanks a lot for your help :thumbsup: :thumbsup:
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.