Hey, welcome to ZBCode, the premier coding forum for ZB. Here you fill find some of the best Invisionfree and Zetaboards Codes on the network! Unfortunately, you're sorta hovering around in guest mode at the moment; why not join in on the fun? Register an account and you can start accessing the wealth of resources we have available here. Enjoy your stay at ZBCode, and remember to tell all your friends about us; the more members, the more codes available. ![]() Interested in joining? Click here. If you are already a member of ZBCode, feel free to login right here: |
| A rather newb question, admittedly | |
|---|---|
| Tweet Topic Started: Aug 14 2008, 06:40 PM (268 Views) | |
| Reid | Aug 14 2008, 06:40 PM Post #1 |
![]()
What? The land of the free? Whoever told you that was your enemy.
![]()
|
I've been doing some small-time JS for a while (nothing major, just dabbling). However, the one thing I see in scripts a lot is RegExp.$1. I've read a few things on what it is, but I can never seem to quite understand it because I'm a visual person. I can't comprehend what objects may do or what they may store without some type of example. I've looked around and found some stuff, but nothing that could tell me what it does or how it is used. So please, I've been dying to know. What exactly is RegExp.$1, and could you please include an example? ![]() Thanks in advance |
| The Resource Board | |
![]() |
|
| Ryura | Aug 14 2008, 08:33 PM Post #2 |
![]()
|
Regular Expressions, or RegExp, basically act as wildcards or match terms for strings. You can use Regular expressions in some search engines to indicate you're not sure what word may be there - ie "john r.[ab]" might find all terms with john ra or john rb in them. In programming, they are more often used as search terms in a string. For example, if you had a string a6bc7defg14h546ij6k8lm and you wanted to extract all the numbers out of that and store it in another string, you might do a search expression on the string and then define var _numbers = RegExp.$1 It's a hard concept to grasp and employ. You can find a reference at http://w3schools.com/jsref/jsref_obj_regexp.asp |
![]() |
|
| slayer766 | Aug 15 2008, 12:26 AM Post #3 |
![]()
Hello all
![]()
|
RegExp.$1 is matching the first set of grouping RegEx, it's number is defined by the order of which group is being matched. Keep in mind that if you are matching for a set of values separately you may only use the RegEx up to 9, anything beyond that will return "undefined". Which is this place you would have to change your method of matching. ![]() Just think of the RegEx virtually storing a matched variable and you can call upon it anytime you would like, while referencing the specific RegEx. Example:
(Also escaping any special characters) That will match in the variable "blah" for whats between the equal sign and the bracket( ] ). It will also match what is between the [Group] tags. The first matched group of that string would be the RegExp.$1, alerted would display what is there, the same with the next RegEx but this time this would be the second RegEx - RegExp.$2 would display/alert what is between the [Group] tags. |
![]() |
|
| Reid | Aug 15 2008, 08:34 AM Post #4 |
![]()
What? The land of the free? Whoever told you that was your enemy.
![]()
|
Ah, that makes a little more sense now. I'll play around with it and hopefully come to master the concept. Thanks. |
| The Resource Board | |
![]() |
|
| Viral | Aug 15 2008, 03:07 PM Post #5 |
![]()
|
To store something in the RegExp object, you need to enclose it with () . So like:
RegExp.$1 now equals fewbfyewu. If you ever need to use the brackets, but don't want to store it in the object, do the following:
The ?: stops it from being stored. |
![]() | |
![]() |
|
| 1 user reading this topic (1 Guest and 0 Anonymous) | |
| « Previous Topic · Code University · Next Topic » |



















7:45 PM Jul 10