Welcome Guest [Log In] [Register]
Search Members Calendar | Rules ZB Code Index IF Code Index
ZBCode
  • Navigation
  • ZBCode
  • Coding Support
  • Code University
  • A rather newb question, admittedly
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:

Username:   Password:
Locked Topic
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
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
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? :P

Thanks in advance
The Resource Board
Offline Profile Goto Top
 
Ryura Aug 14 2008, 08:33 PM Post #2
Member Avatar


Posts:
38
Group:
Member
Member
#11
Joined:
Jul 8, 2008
Coding language
Ruby on Rails
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
Offline Profile Goto Top
 
slayer766 Aug 15 2008, 12:26 AM Post #3
Member Avatar
Hello all

Posts:
1,653
Group:
Distinguished Coder
Member
#12
Joined:
Jul 9, 2008
Coding language
PHP
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:

Code:
 

<script type="text/javascript">
var blah = "[Group=Admin]Type[/Group]";
if(blah.match(/\[Group=(.*)\](.*)\[\/Group\]/i)){
alert(RegExp.$1);
}
</script>

(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.
Offline Profile Goto Top
 
Reid Aug 15 2008, 08:34 AM Post #4
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
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
Offline Profile Goto Top
 
Viral Aug 15 2008, 03:07 PM Post #5


Posts:
342
Group:
Dedicated
Member
#48
Joined:
Jul 17, 2008
Coding language
PHP
To store something in the RegExp object, you need to enclose it with () .

So like:

Code: HTML
 
var a = "fewbfyewu";
if(a.match(/(.*)/gim)){}


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:

Code: HTML
 
var a = "fewbfyewu";
if(a.match(/(?:.*)/gim)){}


The ?: stops it from being stored.

Posted Image
Offline Profile Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Code University · Next Topic »
Locked Topic

Track Topic · E-mail Topic Time: 7:45 PM Jul 10
Hosted for free by ZetaBoards · Privacy Policy