From: Zach Dennis Date: 2005-01-19T02:29:37+09:00 Subject: Re: My regexp stupidity needs assistance before loose all my hair! Bertram Scharpf wrote: > Hi, > > Am Dienstag, 18. Jan 2005, 06:26:35 +0900 schrieb Douglas Livingstone: > >>I think that this is what you need: /\[[\w]+\]/ > > > What are the square brackets for? As far as I see /\[\w+\]/ > does, too. In a regular expression squares brackets represent a character class. A charcter class looks for one character matching any of the characters that make up that character class. Say you are looking for the words "fix" or "fox" in sentence. You could write: /f(i|o)x/ or you could write: /f[io]x/ You can also negate a character class, and match anything that is NOT in the character class. You do this by starting your character class with a carrot ^ Say you wanted to find anything f-x, but not "fox" /f[^o]x/ this will find "fix", "fex", "fux", "fgx", etc.. but not "fox". In the regular expression: /\[[\w]+\]/ \[ = you are looking for a literal left square bracket [\w]+ = you are looking for a character class with any word character one or more times \] = you are looking for a closing right square bracket This will find the "fix" in the sentence "This is a [fix]", but this regular expression will fail if you do "This is a [ fix ]", because the spaces before the "f" and after the "x" are not considered word characters. A better regular expression is (sorry Doug, I"m taking it back, I like mine better now): /\[([^\]]*)\]/ which will match anything inside of square brackets. This will match: "This is a [fix]" $1 will equal "fix" "This is a [ fix ]" $1 will equal " fix " "This is a [ *sentence inside of a fix* ]" $1 will equal " *sentence inside of a fix* " I hope this was helpful. Zach