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.
That's because * is a regex operator meaning zero or more occurrences of the character or subexpression that precedes it.
(. *?) 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.
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.
\\s - matches single whitespace character. \\s+ - matches sequence of one or more whitespace characters.
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.
/([^.]*)\.(.*)/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With