After talking so much about it, I missed an important point: what is the boundary?
Generally, the boundary is a space, the first line of a paragraph, the end of a paragraph, a comma, a period, and other symbols. It is worth noting that the separator "-" can also be used as the boundary.
The regular expression is as follows:
\bmagic\b
Effect demo welcome to nowa-magic this magic place!
This example code
function reg_replace(){var test = document.getElementById("test");var regex = /\b(magic)\b/g;test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>$1</span>");}
Why? In fact, it is easy to understand, from the literal meaning of "-": separator, you can think of it. In fact, it is used to separate words in English.
Here is an important search engine optimization knowledge. You have noticed the naming conventions in this document. I use regular-expression-tutorial.htm. What is the name regular_expression_tutorial.pdf for the division of the lower line? Because when the search engine sees "-", it will be treated as a space "", and when it sees the underscore "_", it will be treated as an empty character "". In fact, the correct name of the underline is "hyphen ". So when I named the Regular-Expression-Tutorial.pdf, what the search engine saw was: Regular Expression Tutorial.pdf, And when I named Regular_Expression_Tutorial.pdf, the search engine was treated as RegularExpressionTutorial.pdf.
It can be seen that the regular expression handles "-" in the same way as the search engine on the Character boundary issue.
Relativity of boundaries:
- When you set a boundary for a common character, such as "s", its boundary is such as space, separator, comma, and period.
- When you set a boundary for a boundary, such as the separator "-" or ",", its boundary is a common character.
The regular expression is as follows:
\b(,)\b
Effect demonstration: welcome to bkjia, this magic place!
The only difference is: This time we match the comma ",", and it is also a boundary, and the result is completely different from the above. It can be seen that for ",", its boundary is a common letter.
The relativity of the boundary is very important, because we often need to match characters such as "<.
Additional reading
The topic list of this article is as follows:
- What is a regular expression?
- Getting started with regular expressions: match a Fixed Single Character
- Getting started with regular expressions: matching any single character
- Getting started with regular expressions: Use character groups
- Getting started with regular expressions: Use character ranges in character groups
- Getting started with regular expressions: Use of assense character groups
- Getting started with regular expressions: matching null characters
- Getting started with regular expressions: Match one or more characters
- Regular Expression: matches zero or multiple characters.
- Regular Expression entry: matches zero or one string.
- Getting started with regular expressions: Match fixed numbers of Characters
- Getting started with regular expressions: match the number of characters in a range
- Getting started with regular expressions: greedy matching
- Getting started with regular expressions: inert matching
- Entry to Regular Expressions: two matching Modes
- Getting started with regular expressions: match word boundaries
- Getting started with regular expressions: boundary definition and relativity
- Getting started with regular expressions: Match non-word boundaries
- Getting started with regular expressions: match the beginning and end of a text
- Entry to regular expression: submode
- Regular Expression entry: "or" Match
- Getting started with regular expressions: replacing with referenced text
- Getting started with regular expressions: unmatched
- Regular Expression Summary: Regular Expressions in JavaScript
- Regular Expression Summary: advanced application of regular expressions in js