Slide14 RegularExpression
Slide14 RegularExpression
- Jamie Zawinski
Regular expression
• console.log(/abc/.test("abcde"));
// → true
• console.log(/abc/.test("abxde"));
// → false
Testing for matches
• console.log(/[0123456789]/.test("in 1992"));
// → true
• console.log(/[0-9]/.test("in 1992"));
// → true
Sets of characters
• Putting {4} after an element, for example, requires it to occur exactly four
times.
• It is also possible to specify a range this way: {2,4} means the element must
occur at least twice and at most four times.
• You can also specify open-ended ranges when using braces by omitting the
number after the comma. So, {5,} means five or more times.
Repeating parts of a pattern
• console.log(dateTime.test("1-30-2003 8:45"));
// → true
Grouping subexpressions
• console.log(cartoonCrying.test("Boohoooohoohooo"));
// → true
Grouping subexpressions
• The first and second + characters apply only to the second o in boo and
hoo, respectively. The third + applies to the whole group (hoo+),
matching one or more sequences like that.
• The i at the end of the expression in the example makes this regular
expression case insensitive, allowing it to match the uppercase B in the
input string, even though the pattern is itself all lowercase.
Matches and groups
• console.log(match);
// → ["100"]
• console.log(match.index);
// → 8
Matches and groups
// → ["100"]
Word and string boundaries
• If we want to enforce that the match must span the whole string,
we can add the markers ^ and $. The caret matches the start of
the input string, whereas the dollar sign matches the end
• /^\d+$/
• /^!/
• /x^/
Word and string boundaries
• If, on the other hand, we just want to make sure the date
starts and ends on a word boundary, we can use the marker
\b.
Word and string boundaries
• console.log(/cat/.test("concatenate"));
// → true
• console.log(/\bcat\b/.test("concatenate"));
// → false
Choice patterns
• console.log(animalCount.test("15 pigs"));
// → true
• console.log(animalCount.test("15 pigchickens"));
// → false
Choice patterns
• Parentheses can be used to limit the part of the pattern that the pipe
operator applies to, and you can put multiple such operators next to
each other to express a choice between more than two alternatives.
The mechanics of matching
let animalCount = /\b\d+ (pig|cow|chicken)s?\b/;
The replace method
• console.log("papa".replace("p", "m"));
// → mapa
The replace method
• The first argument can also be a regular expression, in which case the
first match of the regular expression is replaced. When a g option (for
global) is added to the regular expression, all matches in the string
will be replaced, not just the first.
The replace method
• console.log("Borobudur".replace(/[ou]/, "a"));
// → Barobudur
• console.log("Borobudur".replace(/[ou]/g, "a"));
// → Barabadar
The search method
• console.log(" word".search(/\S/));
// → 2
• console.log(" ".search(/\S/));
// → -1
Summary
• /abc/ A sequence of characters
• /(abc)/
Summary
• /(abc)/ A group
• https://eloquentjavascript.net/
• https://www.w3schools.com/