Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does /([^.]*)\.(.*)/ mean?

Tags:

regex

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 Find & replace jquery. I'm newbie in javascript, so I wonder, what does it mean? Thanks.

like image 885
James Rudolph Avatar asked Dec 04 '09 04:12

James Rudolph


People also ask

What does * represent in regex?

That's because * is a regex operator meaning zero or more occurrences of the character or subexpression that precedes it.

What does .*?) Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.

What does \\ S stand for in Java?

The regular expression \s is a predefined character class. It indicates a single whitespace character. Let's review the set of whitespace characters: [ \t\n\x0B\f\r] The plus sign + is a greedy quantifier, which means one or more times.

What does \\ s+ mean in Java?

\\s - matches single whitespace character. \\s+ - matches sequence of one or more whitespace characters.


2 Answers

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.

like image 157
Rakesh Juyal Avatar answered Oct 20 '22 00:10

Rakesh Juyal


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

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.

like image 32
Vegard Larsen Avatar answered Oct 19 '22 22:10

Vegard Larsen