Happy JS Regular expression (ii)

Source: Internet
Author: User

In the previous article, a test method was introduced, and in this article a different one is used, and the Exec method can find the matching result and return the result and location. EXEC ("regular");

Simple test:

var str = "{Chasing the Dream Child}"; var reg =/chasing dream son///["Chasing Dream Son", index:1, Input: "{Chasing Dream son}"]

EXEC returns an array, the first bit is the match to the content, the second bit is the index that matches to the first position of the content, the index starts at 0.

In addition, if you need to match a piece of text and extract a word, you can give the child braces that need to be extracted.

var str = "{Chasing the Dream Child}"; var reg =/Chase (Dream) Sub///["Chasing Dream Son", "dream", index:1, Input: "{Chasing Dream son}"]

Some people call it a group, and more exec uses it in detail in the following sections, because we need more knowledge to expand our brains.

The. Character can be used to match any one of the strings.

var str = "#f3f3f3"; var reg =/.///["#", index:0, Input: "#f3f3f3"]

Small task: We need to match var str = "Json.js"; this text, and. JS front can be any content, warm hint + can match any string containing at least one n.

var str = "Json.js"; var reg =/.+\.js///["Json.js", index:0, Input: "Json.js"]

If you use a special character in a regular expression, it is best to translate it before and \. The so-called special character is a word regular there is a special meaning, such as +,. , *,? and so on.

If we want to extract the characters in front of the. js you can do so.

var str = "Json.js"; var reg =/(. +) \.js///index.js:3 ["Json.js", "JSON", index:0, Input: "Json.js"]

Recall the groupings we said earlier, and of course we can use multiple groupings.

var str = "Json.js"; var reg =/(. +) \. (JS)///["Json.js", "JSON", "JS", index:0, Input: "Json.js"]

As for what this has to say in the back, do not worry.

^ indicates that the character after this character must be at the beginning of the current position.

var str = "Wjson.js"; var reg =/^j.+\. (JS)///null

' Cause J's not in the starting position.

var str = "Json.js"; var reg =/^j.+\. (JS)///["Json.js", "JS", index:0, Input: "Json.js"]

^ There is another use, if used in [] ^ then ^ is to take the non.

var str = "Jquery.js"; var reg =/j[^a-z][a-z]*.js///["Jquery.js", index:0, Input: "Jquery.js"]

[^a-z] represents any character other than a lowercase a-Z, and the code also uses the * number, which indicates that the preceding character is matched 0 or more times. Here is a match [a-z] of any one of the characters 0 or more times.

The end of this section.

  

  

Happy JS Regular expression (ii)

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.