html
.
replace
(
/<a .*?<\/a>/ig
,
function
(
m
) {
return
sanitizeATag
(
m
);
});
We can simplify this even further—considering that the function
sanitizeATag
matches exactly what
String.prototype.replace
expects, we can rid ourselves of
the anonymous function, and use
sanitizeATag
directly:
html
.
replace
(
/<a .*?<\/a>/ig
,
sanitizeATag
);
Hopefully the power of this functionality is clear. Whenever you find yourself faced
with a problem involving matching small strings within a bigger string, and you need
to do processing on the smaller strings, remember that you can pass a function to
String.prototype.replace
!
Anchoring
Very often, you’ll care about things at the beginning or end of a string, or the entire
string (as opposed to just a part of it). That’s where anchors come in. There are two
anchors—
^
, which matches the beginning of the line, and
$
, which matches the end
of the line:
const
input
=
"It was the best of times, it was the worst of times"
;
const
beginning
=
input
.
match
(
/^\w+/g
);
// "It"
const
end
=
input
.
match
(
/\w+$/g
);
// "times"
const
everything
=
input
.
match
(
/^.*$/g
);
// sames as input
const
nomatch1
=
input
.
match
(
/^best/ig
);
const
nomatch2
=
input
.
match
(
/worst$/ig
);
There’s one more nuance to anchors that you need to be aware of. Normally, they
match the beginning and end of the whole string, even if you have newlines in it. If
you want to treat a string as multiline (as separated by newlines), you need to use the
m
(multiline) option:
const
input
=
"One line\nTwo lines\nThree lines\nFour"
;
const
beginnings
=
input
.
match
(
/^\w+/mg
);
// ["One", "Two", "Three", "Four"]
const
endings
=
input
.
match
(
/\w+$/mg
);
// ["line", "lines", "lines", "Four"]
Word Boundary Matching
One of the often-overlooked useful gems of regexes is word boundary matches. Like
beginning and end-of-line anchors, the word boundary metacharacter,
\b
, and its
inverse,
\B
, do not consume input. This can be a very handy property, as we’ll see
shortly.
A word boundary is defined where a
\w
match is either preceded by or followed by a
\W
(nonword) character, or the beginning or end of the string. Imagine you’re trying
to replace email addresses in English text with hyperlinks (for the purposes of this
Anchoring | 253