This page is not complete.
Boundaries Indicate the beginnings and endings of lines and words.
Matches beginning of input. If the multiline flag is set to true, also matches immediately after a line break character. For example,
This character has a different meaning when it appears at the start of a group.
Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character. For example,
Matches a word boundary. This is the position where a word character is not followed or preceded by another word-character, such as between a letter and a space. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero.
To match a backspace character ([\b]), see Character Classes.
Matches a non-word boundary. This is a position where the previous and next character are of the same type: Either both must be words, or both must be non-words. Such as between two letters or between two spaces. The beginning and end of a string are considered non-words. Same as the matched word boundary, the matched non-word boundary is also not included in the match. For example,
General overview example.
// Using Regex boundaries to fix buggy string. buggyMultiline = `tey, ihe light-greon apple tangs on ihe greon traa`; // 1) Use ^ to fix the matching at the begining of the string, and right after newline. buggyMultiline = buggyMultiline.replace(/^t/gim,'h'); console.log(1, buggyMultiline); // fix 'tey', 'tangs' => 'hey', 'hangs'. Avoid 'traa'. // 2) Use $ to fix mathing at the end of the text. buggyMultiline = buggyMultiline.replace(/aa$/gim,'ee.'); console.log(2, buggyMultiline); // fix 'traa' => 'tree'. // 3) Use \b to match characters right on border between a word and a space. buggyMultiline = buggyMultiline.replace(/\bi/gim,'t'); console.log(3, buggyMultiline); // fix 'ihe' but does not touch 'light'. // 4) Use \B to match characters inside borders of an entity. fixedMultiline = buggyMultiline.replace(/\Bo/gim,'e'); console.log(4, fixedMultiline); // fix 'greon' but does not touch 'on'.
Matching beging of an input by a ^ control character.
^ for average matching begining of a word. In this example get fruites that starts with 'A' by a
/^A/ regex. Here
^ plays only one role: show begingin of the input. For selecting appropriate fruits used filter method with an arrow function.
let fruits = ["Apple", "Watermelon", "Orange", "Avocado", "Strawberry"]; // Select fruits started with 'A' by /^A/ Regex. // Here '^' control symbol used only in one role: Mathcing begining of an input. let fruitsStartsWithA = fruits.filter(fruit => /^A/.test(fruit)); console.log(fruitsStartsWithA); // [ 'Apple', 'Avocado' ]
It the second example
^ used for both: matching begingin of the input and as negated or complemented character set when it used in groups.
let fruits = ["Apple", "Watermelon", "Orange", "Avocado", "Strawberry"]; // Selecting fruits that dose not started by 'A' by a /^[^A]/ regex. // In this example two meaning of '^' control symbol represented: // 1) Matching begining of the input // 2) A negated or complemented character set: [^A] // That is, it matches anything that is not enclosed in the brackets. let fruitsStartsWithNotA = fruits.filter(fruit => /^[^A]/.test(fruit)); console.log(fruitsStartsWithNotA); // [ 'Watermelon', 'Orange', 'Strawberry' ]
Matching a word boundary.
let fruitsWithDescription = ["Red apple", "Orange orange", "Green Avocado"]; // Select descriptions that contains 'en' or 'ed' words endings: let enEdSelection = fruitsWithDescription.filter(descr => /(en|ed)\b/.test(descr)); console.log(enEdSelection); // [ 'Red apple', 'Green Avocado' ]