tags:

views:

344

answers:

11

When I searched about something, I found an answered question in this site. 2 of the answers contain

/([^.]*)\.(.*)/

on their answer. The question is located at http://stackoverflow.com/questions/233922/find-replace-jquery. I'm newbie in javascript, so I wonder, what does it mean? Thanks.

+2  A: 

It's a regular expression that roughly searches for a string that doesn't contain a period, followed by a period, and then a string containing any characters.

John Weldon
+6  A: 

It's a regular expression (it matches non-periods, followed by a period followed by anything (think "file.ext")). And you should run, not walk, to learn about them. Explaining how this particular regular expression works isn't going to help you as you need to start simpler. So start with a regex tutorial and pick up Mastering Regular Expressions.

Jason
+1 for saying "learn about regexes."
Chris Lutz
-1 for not mentioning groups, the whole point of this expression.
UncleO
@uncleo: Fine, but that wasn't the point of his question. He didn't even realize that it was a regular expression so explaining the intricacies of this particular regular expression isn't going to help him much.
Jason
+3  A: 

Anything except a dot, followed by a dot, followed by anything.

You can test regex'es on regexpal

hasen j
+2  A: 

That is a regular expression. Regular expressions are powerful tools if you use them right.

That particular regex extracts filename and extension from a string that looks like "file.ext".

Anon.
or `.textfile` (first part is optional)
Amarghosh
The last part is optional too. `.` would match too. And `...........` would match on the first `.` as well.
eyelidlessness
or `Makefile` (the extension is optional) (but the regex won't match that)
Thanatos
+2  A: 

It's a regular expression that splits a string into two parts: everything before the first period, and then the remainder. Most regex engines (including the Javascript one) allow you to then access those parts of the string separately (using $1 to refer to the first part, and $2 for the second part).

Yuliy
+9  A: 
/([^.]*)\.(.*)/

Let us deconstruct it. The beginning and trailing slash are delimiters, and mark the start and end of the regular expression.

Then there is a parenthesized group: ([^.]*) The parentheseis are there just to group a string together. The square brackets denote a "character group", meaning that any character inside this group is accepted in its place. However, this group is negated by the first character being ^, which reverse its meaning. Since the only character beside the negation is a period, this matches a single character that is not a period. After the square brackets is a * (asterisk), which means that the square brackets can be matched zero or more times.

Then we get to the \.. This is an escaped period. Periods in regular expressions have special meaning (except when escaped or in a character group). This matches a literal period in the text.

(.*) is a new paranthesized sub-group. This time, the period matches any character, and the asterisk says it can be repeated as many times as needs to.

In summary, the expression finds any sequence of characters (that isn't a period), followed by a single period, again followed by any character.

Edit: Removed part about shortening, as it defeats the assumed purpose of the regular expression.

Vegard Larsen
More than increasing readability, this kind of defeats the purpose of the regex.Which is likely to extract the parts of the string before and after the first period.
Anon.
A: 

the . character matches any character except line break characters the \r or \n.

the ^ negates what follows it (in this case the dot)

the * means "zero or more times"

the parentheses group and capture,

the \ allows you to match a special character (like the dot or the star)

so this ([^.]*) means any line break repeated zero or more times (it just eats up carriage returns).

this (.*) part means any string of characters zero or more times (except the line breaks)

and the \. means a real dot

so the whole thing would match zero or more line breaks followed by a dot followed by any number of characters.

For more information and a really great reference on Regular Expressions check out: http://www.regular-expressions.info/reference.html

Joe Basirico
[^.] matches anything but a literal dot character '.' .
UncleO
is this a JavaScript specific syntax? What's the difference between ^. and \. ?
Joe Basirico
@Joe Basirico: `\.` matches a literal dot, `^.` inside a character class like this `[^.]` matches anything which is **not** a literal dot.
kemp
+1  A: 

This is a regular expression with some advanced use.

Consider a simpler version: /[^.]*\..*/ which is the same as above without parentheses. This will match just any string with at least one dot. When the parentheses are added, and a match happens, the variables \1 and \2 will contain the matched parts from the parentheses. The first one will have anything before the first dot. The second part will have everything after the first dot.

Examples:

input: foo...bar
\1: foo
\2: ..bar

input: .foobar
\1:
\2: foobar
Joy Dutta
+1  A: 

This regular expression generates two matching expressions that can be retrieved.

The two parts are the string before the first dot (which may be empty), and the string after the first dot (which may contain other dots).

The only restriction on the input is that it contain at least one dot. It will match "." contrary to some of the other answers, but the retrived groups will be empty.

UncleO
A: 

It's a regular expression, which basically is a pattern of characters that is used to describe another pattern of characters. I once used regexps to find an email address inside a text file, and they can be used to find pretty much any pattern of text within a larger body of text provided you write the regexp properly.

cornjuliox
+1  A: 

Original: /([^.]*)\.(.*)/

Split this as:
[1] ([^.]*) : It says match all characters except . [ period ]
[2] \. : match a period
[3] (.*) : matches any character

so it becomes [1]Match all characters which are not . [ period ] [2] till you find a .[ period ] then [3] match all characters.

Rakesh Juyal