Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions in Javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use regular expressions in Javascript. I hope you will get something after reading this article. Let's discuss it together.

Regular expressions-introduction

Regex originated in neuroscience and mathematics, and it was not until 1968 that Ken Thompson was implemented programmatically in an QED text editor for text search. It is now part of many programming languages, such as perl, Java, Python, Ruby, and JavaScript.

Let's look at some examples of how regular expressions work.

I will use JavaScript in my example. Now, to pass the elementary level, you need to learn all the characters, classes, quantifiers, modifiers, and methods to use in regular expressions. This is the link to the regular expressions page in the JavaScript tutorial, where you can view the table that contains all of this. You can also refer to the cheat sheet at the end of this article, which contains the most commonly used characters.

The following is an example of a match in a highlighted line:

Basket, bulb,B12 vitamin, BaSO4, N BC company

The above regular expression will stop searching for "Basket" and return a positive reply. This is because if you want the regular expression to see all possible matches, you must specify the global modifier'g'.

Now, let's take a look at how to use the expression JavaScript. The test method has a cloud: if a matching return true is found, otherwise false.

Var input = "your test string", regex = / B [a-zA-Z\ d] + /; if (! regex.test (input)) alert ('No match is found'); elsealert ('A match is found')

Let's try another approach: match returns the match found in the array.

Var input = "your test string", regex = / B [a-zA-Z\ d] + / g, / * I've added the global modifier 'g'to the regex to get all the matches*/ary = input.match (regex); if (ary===null) alert ('No match is found'); elsealert ('matches are:' + ary.toString ())

What about the string replace? Now let's try regular expressions.

Var input = "your test string", regex = / B [a-zA-Z\ d] + / input.replace (regex, "#"); regular expression exercises

For exercises, you can search for "regular expression exercises" on Baidu, Google, etc., and try to solve them. According to the level of difficulty, here are the expected results when you try these exercises.

Basic

For me, being able to verify passwords is enough for beginners. Therefore, verify the password length of 8 to 16 characters, alphanumeric, allowing you to select special characters.

In the middle

This is where you should practice more real-world data and learn more regular expression points, such as foresight, rearview assertions, and matching groups.

Verify PIN code, hexadecimal, date, email ID, floating point number.

Replace trailing zeros, spaces, a set of matching words

Extract different parts of URL

Advanced

You can optimize the solution to the above exercise-the optimized e-mail regular expression contains thousands of characters-so it's enough to use it as long as you feel comfortable. You can also try:

Parsing HTML or XML (although this is discouraged in the real world, because using regular expressions to parse an irregular language like HTML is never foolproof. Plus XML parsing is a daunting task, which is more suitable for advanced users)

Replace label

Delete comments (except IE conditional comments)

Regular expression tool

The tool for visualizing regular expressions is one of the coolest things for me. If you have encountered very long complex regular expressions, just copy and paste them into one of the tools, and you can clearly see the flow. In addition, you can use many tools to deal with regular expression code. They also show examples and cheat sheets as well as sharing capabilities.

Debuggex-it will draw a regular expression graph based on your input, and you can quickly share it to StackOverflow from there.

RegExr-you can use this to test your regular expressions. It also provides references, cheat sheets, and examples to help you.

Regular expression memo single token defines [ABC] any single character a, b or c [^ ABC] any character except a, b or c [az] characters between (including) a to z [^ az] characters except a to z [AZ] characters between An and Z. Any single character\ s any blank character\ s any non-white space character\ d any number 0 to 9\ D any non-numeric\ w any word character (letters, numbers and underscores)\ W any non-word character (…) Capture all closed things (a | b) match an or ba? The character an either does not exist, or appears once a * character an either does not exist, or multiple times a + character an appears one or more times {3} character an appears three times in a row {3,} character an appears 3 or more times {3Power6} character an appears 3 to 6 times in a row, the beginning of ^ string, the end of string\ b, the boundary of one word. If the character is the last or first word character of a word, or if the character is between words or non-word characters\ B non-word boundary after reading this article, I believe you have some understanding of "how to use regular expressions in Javascript". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report