Getting started with regular expressions: boundary definition and relativity

Source: Internet
Author: User

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:

  1. What is a regular expression?
  2. Getting started with regular expressions: match a Fixed Single Character
  3. Getting started with regular expressions: matching any single character
  4. Getting started with regular expressions: Use character groups
  5. Getting started with regular expressions: Use character ranges in character groups
  6. Getting started with regular expressions: Use of assense character groups
  7. Getting started with regular expressions: matching null characters
  8. Getting started with regular expressions: Match one or more characters
  9. Regular Expression: matches zero or multiple characters.
  10. Regular Expression entry: matches zero or one string.
  11. Getting started with regular expressions: Match fixed numbers of Characters
  12. Getting started with regular expressions: match the number of characters in a range
  13. Getting started with regular expressions: greedy matching
  14. Getting started with regular expressions: inert matching
  15. Entry to Regular Expressions: two matching Modes
  16. Getting started with regular expressions: match word boundaries
  17. Getting started with regular expressions: boundary definition and relativity
  18. Getting started with regular expressions: Match non-word boundaries
  19. Getting started with regular expressions: match the beginning and end of a text
  20. Entry to regular expression: submode
  21. Regular Expression entry: "or" Match
  22. Getting started with regular expressions: replacing with referenced text
  23. Getting started with regular expressions: unmatched
  24. Regular Expression Summary: Regular Expressions in JavaScript
  25. Regular Expression Summary: advanced application of regular expressions in js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.