"a string"
, 'another string'
.The plus operator is overloaded to cause string concatenation. Other values will be coersed to strings if necessary.
"hi" + 0; // "hi0"
0 + "hi"; // "0hi"
"1" + 0; // "10"
Note: Oftentimes you read numeric input from a text field. You must convert it into a number first, to avoid examples like the above.
"hi there"[4] == "h"
, "hello".length == 5
. There is no separate type for single characters.Strings are immutable: You cannot change their value, you can only create a new string.
indexOf
, but finds the last occurence instead.
A regular expression literal is marked by two forward slashes: /stuffHere/
. The second slash may optionally be followed by the letters “i”, “g”, and/or “m”, which affect how the expression behaves.
Between the two forward slashes, a number of elements can be present, each with its meaning. Here are the most important ones:
\\.
, \\?
, \\*
, \\[
.
[a-z.0-9]
, which matches any lowercase character or digit or dot.
/\\bcat\\b/
will match in “12cat” but not in “cats”.
(.)\1
will match an occurence of the same character back-to-back.
Some examples would be in order:
/([\w\-]+\d+)@hanover\.edu/
This will match all student emails at Hanover (faculty/staff emails don’t end in digits). It looks for a word character or dash, one or more times, followed by one or more digits, then a literal at sign, “hanover”, then a literal dot, then “edu”.
/(.)(.)(.)\3\2\1/
This regular expression will match palindromes with total length 6, e.g. something like “abccba”. The parenthesized dots match one arbitrary element each, then the backticked numbers refer to those matches in reverse order.
/((.)(.)(.))\4\3\2\1/
QUESTION: What does the above regex match?
/^\s*[+-]?(?:\d+\.?|\.\d+)\d*(?:[eE][+-]?\d+)?\s*$/
This is a fairly complicated regular expression that matches numbers in decimal or scientific format. It starts with a caret, and ends in a dollar sign, meaning that the contents must match the entire line/string. It allows for an arbitrary number of whitespace characters on either end. After that, it has an optional sign, followed by: either at least one digit followed by an optional dot, or a dot following by at least one digit (these form a non-capturing group), and they are followed by zero or more digits. Finally, there is another optional non-capturing group, that matches an optional exponent. It consists of an uppercase or lowercase e, followed by an optional sign, followed by at least one digit.
/"((?:\\\\|\\"|[^"])*)"/
This is a complicated regular expression used to capture a “double-quoted string” within a string. If you read in a data file or some javascript code, you may find in it some quoted strings. The quoted string ends when we find the next unescaped quote. A quote can be escaped by using a backslash, unless that backslash itself was escaped by another backslash. That is why the above pattern has 3 parts separated by vertical lines to indicate any of the 3 alternatives at any given time. The first part matches two backslashes back to back, the next matches a backslash followed by a quote, the third part matches any character except for a quote. Make sure you think about this and understand why it is necessary to do it this way.
If s
is a string and r
is a regular expression, there are a number of different ways we can try to “apply” the regular expression to the string to see if it matches:
r.exec(s)
r.test(s)
s.match(r)
s.search(r)
s.replace(r,s2)
s2
. Look at the documentation for details on this second argument.
s.split(r)
r
as separators to split the string at.
When working with regular expressions, it is always a good idea to create a list of test strings first, then see how the regular expression behaves on those strings.