Detailed explanation of JS native objects and regular expressions
This article mainly shares with you the detailed explanation of JS native objects and regular expressions. There are string objects, array objects, date objects, mathematical objects and regular expressions respectively. I hope it can help everyone.
Hope it helps everyone.
1. String object
<script type="text/javascript"> // charAt indexOf replace search subStr // subString splite fromCharCode slice var a = "qwe"; console.log(a); var p = a.indexOf("e"); console.log(p); document.write(String.fromCharCode(72,69,76,76,79));</script>
2. Array object
<script type="text/javascript"> // 创建数组 var arr1 = [1,2,3]; var arr2 = new Array(); // 空数组 var arr3 = new Array(5); // 长度为5 var arr4 = new Array("8"); // ["8"] var arr5 = new Array(4,5); // [4,5] console.log(arr1,arr2,arr3,arr4,arr5); // 属性 console.log(arr5.length); // 方法 // pop push 对数组的末尾添加/删除元素 var newLength = arr5.push(6,7,8); arr5.pop(); console.log(arr5, newLength); // unshift shift 对数组首位添加/删除元素 arr5.unshift(9); console.log(arr5); var arr6 = [1,2,3,4]; // 数组反转 把数组元素的顺序反过来 arr6.reverse(); console.log(arr6); // 把数组转化为字符串 var str = arr6.join("-"); console.log(str); // 排序 arr7 = [99, 45, 78, 22, 11, 1, 123]; arr7.sort(function(a, b){ return a - b; }); console.log(arr7); // splice(index,length, item1,item2.....itemn); // 用后面的item覆盖前面index和length组成的范围 var arr8 = [11, 22, 33, 44]; arr8.splice(2, 0, 999); console.log(arr8); </script>
3. Date object
<script type="text/javascript"> // Date var date = new Date("2015-1-2"); var date2 = new Date("2015-1-1"); console.log(date); console.log(date2); // 两个日期相减得到的是时间差,单位是毫秒 var res = date - date2; console.log(res/1000/3600/24 + 1); // get 获取 console.log(date.getFullYear()); // set 设置 </script>
4. Mathematical object
<script type="text/javascript"> // Math // 属性 console.log(Math.PI); // 方法 // 生成0-1的随机数 Math.random(); // abs 返回绝对值 Math.abs(-3); </script>
5. Regular expression
<script type="text/javascript"> // reg = /正则部分(定义的规则)/正则属性 // g 全局匹配 i是否区分大小写 var reg = /test/gi var str = "hellotesthowareTESTyoutest"; console.log(str.match(reg)); // \d 代表数字 var reg2 = /\d/g; var str2 = "wfawsefw2390480asdfas"; console.log(str2.match(reg2)); // \w 代表单词字符 数字 字母 下划线 var reg3 = /\w/g; var str3 = "sgfsd234hjl_ sdfj+ \*`134212asd2( ,:a"; console.log(str3.match(reg3)); // .匹配任意字符(除去换行符 \n) var reg4 = /./g; var str4 = "4hjl_ sdfj+ \t*`1342\n12asd2( ,:a"; console.log(str4.match(reg4)); // \s匹配空格字符 匹配空格 var reg5 = /\s/g; console.log(str4.match(reg5)); // 量词 var reg6 = /\d{1,3}/g; var str6 = "3456787re0wr4645"; console.log(str6.match(reg6)); // 方括号 // 2 3 4 6里面的任何一个 var reg7 = /[2-46{2}]/g; var str7 = "23985734985028040"; console.log(str7.match(reg7)); // 除去123以外的任何一个 var reg8 = /[^123]/g; var str8 = "1234567890"; console.log(str8.match(reg8)); // 小括号 var reg9 = /3((4)|(2))/g; var str9 = "2340250123284081"; console.log(str9.match(reg9)); // 开头和结尾 // ^ $ var reg10 = /^34$/g; var str10 = "342093434"; console.log(str10.match(reg10)); // 11位 纯数字 1开头 var reg11 = /^1(3[01379]|47|5[0258])\d{8}$/g; var str11 = "13045340509"; console.log(str11.match(reg11)); // 邮箱 // (4-10位单词字符 开头必须是字母)@ // @qq @163 . cn com var reg12 = /^[A-Z]|[a-z]\w{3,9}@(qq|163)(.com|.cn)$/g; var str12 = "asdfsf@qq.com"; console.log(str12.match(reg12)); </script>
The above is the detailed content of Detailed explanation of JS native objects and regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

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

As a programming language widely used in the field of software development, C language is the first choice for many beginner programmers. Learning C language can not only help us establish the basic knowledge of programming, but also improve our problem-solving and thinking abilities. This article will introduce in detail a C language learning roadmap to help beginners better plan their learning process. 1. Learn basic grammar Before starting to learn C language, we first need to understand the basic grammar rules of C language. This includes variables and data types, operators, control statements (such as if statements,

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a
