23
loading...
This website collects cookies to deliver better user experience
three
in a text file because we need to replace all instances of three
with the number 3
. You've done a bit of Googling and or checked out regex101.com. You're feeling pretty good so you write out this regular expression.const reMatchThree = /three/g
/
and the ending /
is the regular expression. The g
after the last /
means global, as in find all instances.three
so it can be replaced with 3
. You look at what got replaced in the text and you're a little perplexed.- There were three little pigs who lived in their own houses to stay safe from the big bad wolf who was thirty-three years old.
+ There were 3 little pigs who lived in their own houses to stay safe from the big bad wolf who was thirty-3 years old.
three
got replaced by 3
everywhere in the file, but why was thirty-three replaced? You only wanted three
s replaced. And here we have our first lesson. Be specific. We only want to match when it's only the word three
. So we need to beef up this regex a little. We only want to find the three
when it's the first word in a sentence, has white space before and after it or some punctuation before and/or after it, or if it's the last word in a sentence. With that criteria, the regex might look like this now.const reMatchThree = /\b(three)\b/g
\b
character means a word boundary character.const reMatchBetweenDoubleQuotes = /"(.+)"/g
(
and )
represent a group. The .
character means any character. Another special character is +
. It means at least one character. Hi there "this text is in double quotes". As well, "this text is in double quotes too".
this text is in double quotes". As well, "this text is in double quotes too
.+
which means literally match any character as many times as possible, which is why we end up matching only this text is in double quotes". As well, "this text is in double quotes too
because "
is considered any character. You got greedy, or more specifically the regex did.+
, by replacing it with +?
const reMatchBetweenDoubleQuotes = /"(.+?)"/g
"
, start a capturing group then find as many characters as possible before you hit a "
const reMatchBetweenDoubleQuotes = /"([^"]+)"/g
"
, start a capturing group then find as many characters as possible that aren't "
before you hit a "
.[
and ]
are a way to say match any of the following characters. In our use case, we're using it with ^
, i.e. [^
, to say do not match any of the following things. In our case, we're saying do not match the "
character.