Home Web Front-end JS Tutorial JavaScript Advanced 1 Regular Expressions, Cookie Management, UserData_Basic Knowledge

JavaScript Advanced 1 Regular Expressions, Cookie Management, UserData_Basic Knowledge

May 16, 2016 pm 05:55 PM
javascript

首先,什么事正则表达式呢,其实引入概念很多时候并不能帮我们明白它到底是什么,所以我先简单描述下,正则表达式,其实就是一个记录字符串规则则的字符串,等我们看完这一部分,也就能明白它到底是什么了。

基本语法:正则表达式就是“/expression/”+表示搜索范围的符号。例如我们要找function这个关键词,就是/function/gi,其中g表示global,就是全局搜索,i表示ignor,就是忽略大小写。

在js中,我们通过RegExp类来实现。

这个类里面有很多很多的符号用来表示不同的索引方法,我先把这个表列一列:

元字符 说明 量词 说明 反义字符 说明
  . 匹配除了换行符号(\n)以外的任意字符   * 出现次数:[0,+∞)   \W 字母,数字,下划线,汉字以外的字符
  ^ 匹配字符串的开始   + 出现次数:[1,+∞)   \S 空白字符以外的字符
  $ 匹配字符串的结束   ? 出现次数:[0,1]   \D 数字以外的字符
  \b 匹配单词边界   {n}   出现次数:n   \B 匹配非单词的边界
  \d 匹配数字   {n,} 出现次数:[n,+∞)   [^] 在字符类中,^号后面的字符串以外的任意字符
  \s 匹配任意的空白符   {n,m}   出现次数:[n,m]   [-] 匹配从-前字符到-后字符的字符串中的字符/数字
  \w 匹配字母,数字,下划线或汉字        

In addition to the above symbols, there are three concepts: one is grouping, one is backreference, and the last is candidate.
Grouping: refers to enclosing strings with (), so that strings can be combined according to certain rules. At the same time, brackets can also be nested, such as using regular expressions to express the date format: var dateReg=/^(d{1,4})(-)(d{1,2}(-)(d{1 ,2})$), Of course, this method also has certain loopholes. Here we just express the format problem.
In addition to these, there are square brackets. For example, if you only want to match one of the letters x y z d w, then write [xyzdw]. If the match is continuous, such as numbers 0-4, then [0 -4], but this only represents one character.
If you want to write more than one, such as matching ac or bd, then use the "|" symbol and write (ac|bd).
For example, if we want to match a string containing only abc, we can write: abdReg=/^[abc] $/; The following is a complete example.
Copy code The code is as follows:



regular express






Reverse Directed reference: It is an application of regular expressions based on grouping. First of all, you need to know the group number: follow the order of the groups from left to right, marked by the left bracket, and start accumulating from 1. There are two ways to use it: $group number, or group number.
The second one is suitable for referencing groups in expressions. "" is an escape symbol, which has the same meaning as usual. It is used when matching reserved characters.
For example, we want to match a string that starts with abc, ends with abc, and has no limit in the middle: Reg=/^(abc)[a-z]*1$/; You can try this sentence in the example just now, I Tested and there were no errors.
Several commonly used matching regular expressions:
1. Matching date: reg=/^d{4}-(((0[13578]|(10|12))-(0[1-9] |[1-2]d|3[01]))|(02-(0[1-9])|[1-2]d)|((0[469]|11)-(0[1- 9]|[1-2]d|30)))$/g;
2. Matching time: reg=/^([0-1]d|[2][0-3](:([ 0-5]d)){2}$/g;
3. Matching email address: reg=/^([a-zA-Z]([0-9a-zA-Z]|(_))* @(([0-9a-zA-Z]|(_)) .) [a-zA-Z]{2,9})$/g;
4. Match Chinese characters: reg=/^[ u4e00-u9fa5] $/g;
Javascript operates cookies
I believe everyone knows what cookies are, so I just copied the definition without mercy.
The statements for operating cookies with js are as follows: document.cookie=name "="value ";expires=" date.toGMTString();
Next we will use cookies to record the user name and password for login
Copy code The code is as follows:



cookie test














< /table>




Explain that escape is a simple encryption, expires is the lifetime, which is generally set to one week, and will be automatically deleted after one week.
Of course, if you just write the cookie but don’t read it, it will be in vain, so you need to learn to read the contents of the cookie file.
So we added two reading functions to the script:
Copy the code The code is as follows:



In addition, add a sentence to the body tag onload event:

So, this code executes correctly in IE and FF, but the cookie cannot be retrieved in chrome. . I really can’t figure it out~ Does anyone know why? Please give me some advice orz
Modifying the cookie validity period is the same as modifying the cookie content. It just needs to find the expires field content and then modify it. If you want to delete the cookie, just delete it. Setting the validity period to yesterday is OK.
userData
Different from cookies, userData does not have a validity period and can store more data (640KB cookie: 4KB), so if the page input volume is large, userData can be used to store data.
Save data to userData: There are mainly two parts: give the content a name and save it to userData.
Before learning, please note that this was developed by Microsoft, so it is only applicable to IE. After testing, FF and chrome stated that this function is not compatible.
Here is a picture about the stored almanac:

OK Next, let’s take a look at userData.
First add a style before the script, and then save it to userData by setting parameters:
Copy code The code is as follows:



cookie test





user name:
password:











title:
content:



🎜>


Copy code
The code is as follows: function getContent(){ var formObj=document. form; var contentObj=formObj.content;
contentObj.load("contentData");
contentObj.value=contentObj.getAttribute("contentText");
}


The complete program after modification:



Copy the code
The code is as follows:



cookie test

















title:
content:







好接下来说个兼容还算可以的:(下面这段都是抄的,因为转载太多我也不知道源出处了。)
localStorage: 相对于其他方案,localStorage有自身的优点:容量大、易用、强大、原生支持;缺点是兼容性差些(chrome, safari, firefox,IE 9,IE8都支持 localStorage,主要是IE8以下版本不支持)、安全性也差些(所以请勿使用localStorage保存敏感信息)。

非常通俗易懂的接口:

localStorage.getItem(key):获取指定key本地存储的值
localStorage.setItem(key,value):将value存储到key字段
localStorage.removeItem(key):删除指定key本地存储的值

留意localStorage存储的值都是字符串类型,在处理复杂的数据时,比如json数据时,需要借助JSON类,将json字符串转换成真正可用的json格式,localStorage第二个实战教程会重点演示相关功能。localStorage还提供了一个storage事件,在存储的值改变后触发。
目前浏览器都带有很好的开发者调试功能,下面分别是Chrome和Firefox的调试工具查看

报废了好久,终于抖擞精神把进阶1写好了,以后不能再这么堕落了哎。。闭关修炼还是必须的~。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles