Let's take a simple example.
The regular expression is as follows:
magic
Effect demo welcome to bkjia this magic place!
This example code
function reg_replace(){var test = document.getElementById("test");aim = "magic";var regex = new RegExp("("+aim+")","g");test.innerHTML = test.innerHTML.replace(regex,"<span style='background-color:orange'>$1</span>");}
Obviously, we usually only want to match magic, instead of the magic that appears in bkjia.
In a regular expression, you can add "\ B" before the character to match the character following it at the first place of the string.
The character mentioned later refers to a single character (such as "j"), a character group (such as "[abcde]"), and a specific character type (such as "\ d "), escape special character "\ [" or a single arbitrary character (that is, ". ").
Let's take a look at this example:
Effect demo welcome to bkjia this magic place!
This time, the matching is correct. With the example of a single character just now, this expression is well understood: "\ B" only specifies that the character "m" must appear in the first place of the string, next, the characters a, g, I, and c are required. The matching of these two characters is irrelevant to "\ bm". They belong to the category of fixed character matching.
In a regular expression, you can add "\ B" after the character to match the character at the end of the string.
\ Bmagic \ B. This regular expression must start with m and end with c.
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