Jump to content

Regular Expressions


Ingolme

Recommended Posts

I have hardly looked at regular expressions before. I saw them when W3Schools added them to the Javascript reference in September.I've been trying to make some sense out of it, but some of the things aren't explained well anywhere.I've been seeing that you can replace certain things in strings by writing $1, $2, $3... But how do I know exactly which part of the Regular Expression is assigned to each one?Aside from that, could somebody give me a series of progressively more difficult expressions and the explanations of how they're working? I was attempting to use RewriteCond and RewriteRule in an htaccess file on my computer, but they don't seem to ever work and return Internal Server Error, so I think I'm doing things wrong.

Link to comment
Share on other sites

I've found this site to be the most helpful in learning regular expressions:http://www.regular-expressions.info/As for the $1, $2, etc., they are references to groups or captures which are defined in your expressions with parentheses. $1 represents the group that is defined with the first left parenthesis, $2 is the second, $3 the third and so on.I can't seem to come up with a decent example this morning (still drinking my coffee), so I copied one from here - http://developer.mozilla.org/en/docs/Core_...:String:replace

var re = /(\w+)\s(\w+)/;var str = "John Smith";var newstr = str.replace(re, "$2, $1");alert(newstr);

Link to comment
Share on other sites

One of my famous regular expression is:

You're looking in the engine compartment for a dent on the passenger door.

:)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...