Home > Web Front-end > JS Tutorial > Detailed explanation of JavaScript regular expressions and cascading effects

Detailed explanation of JavaScript regular expressions and cascading effects

巴扎黑
Release: 2017-09-16 09:49:59
Original
1681 people have browsed it

Regular expression (regular expression) is a string matching pattern, used to check whether a string contains a string of a specified pattern. Let’s share JavaScript_regular expressions and cascading effects with everyone through this article. Friends who are interested should take a look together

1. Regular expression (regular expression)

is a string matching pattern, used to check whether a string contains a string of the specified pattern.

2. Creation of regular expressions


##

var reg = /white/;
var reg = new RegExp("white","g");
Copy after login

3. The regular expression modifier

g performs global matching (finding all matches instead of stopping after the first match is found).

i Case-insensitive
m Multi-line matching

4. Regular expression symbols

square brackets : Square brackets are used to find characters within a range:

[abc] Find any characters between square brackets.

[^abc] Find any characters not between square brackets.
[0-9] Find any number from 0 to 9.
[a-z] Find any character from lowercase a to lowercase z.
[A-Z] Find any character from uppercase A to uppercase Z.
[A-z] Find any character from uppercase A to lowercase z.
[adgk] Find any character within the given set.
[^adgk] Find any character outside the given set.
(red|blue|green) Find any specified option.

Metacharacters: Metacharacters are characters with special meanings:

/…/ represents the beginning and end of a pattern

^ matches the beginning of a string
$ Matches the end of the string
\s Any whitespace character
\S Any non-whitespace character
\d Matches a numeric character, equivalent to [0-9]
\D Except for digits Any character, equivalent to [^0-9]
\w Matches a number, underscore, or alphabetic character, equivalent to [A-Za-z0-9_]
\W Any non-single character character, equivalent In [^a-zA-z0-9_]
. Any character except newline character

Regular expression repeating characters (quantifier)

{n} matches the previous item n times

{n,} Matches the previous item n times, or multiple times
{n,m} Matches the previous item at least n times, but not more than m times
* Matches the previous item 0 times or multiple times, equivalent to {0,}
+ Match the previous item one or more times, equivalent to {1,}
? Matches the previous item 0 or 1 times, which means the previous item is optional, equivalent to {0,1}

5, properties of the RegExp object

global Whether the RegExp object has the flag g, which declares whether the given regular expression performs global matching.

ignoreCase Whether the RegExp object has flag i, which declares whether the given regular expression performs case-insensitive matching.

multiline Whether the RegExp object has the flag m, which declares whether the given regular expression performs multi-line matching.

6. Methods of RegExp object

1. exec searches for characters that match the regular expression, returns the found value, and Determine its location

exec()

The exec() method retrieves a specified value from a string. The return value is the found value. If no match is found, null is returned.

Example 1:


var patt1=new RegExp("e"); document.write(patt1.exec("The best things in life are free")); 由于该字符串中存在字母 "e",以上代码的输出将是:
e
Copy after login

2. test Retrieve the value specified in the string and return true or false

test()

The test() method retrieves the specified value in a string. The return value is true or false.

Example:


var patt1=new RegExp("e"); document.write(patt1.test("The best things in life are free")); 由于该字符串中存在字母 "e",以上代码的输出将是:
True
Copy after login

7. Analysis (email verification) var reg=/^\w+@\w+.[a -zA-Z]{2,3}(.[a-zA-Z]{2,3})?$/;

//Regular expression creation

^Start of string
$End of string
\wAny character letters and numbers, underscore
+ indicates that the previous character appears {1,}, one or more times.
@ Ordinary string
\w Any string ddd@123
. Any character except newline ddd@123.
[a-zA-Z] ddd@123.c ddd @123.n
{2,3} ddd@123.com ddd@123.net ddd@123.tv
(.[a-zA-Z]{2,3})? ddd@123. com.cn ddd@123.net

Commonly used regular rules:

User name regular rules: /^[a-zA-Z][a-zA-Z0-9]{3,15} $/

Password regularity:/^[a-zA-Z0-9]{4,10}$/
Birthday regularity:/^((19\d{2})|(200\d)) -(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/
Email regular: /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/
Postal code:/^\d{6}$/
Mobile phone number :/^1\d{10}$/

8. Methods of String object

match 找到一个或多个正则表达式的匹配
search 检索与正则表达式相匹配的值
replace 替换与正则表达式匹配的字符串
split 把字符串分割为字符串数组
9、select对象常用事件、方法和属性.

1、事件 onchange 当改变选项时调用的事件

2、方法 add() 向下拉列表中添加一个选项

示例:


 var province=document.getElementById("selProvince").value; 
  var city=document.getElementById("selCity");
  city.options.length=0; 
  switch(province){
   case "河南省":
    city.add(new Option("郑州市","郑州市"),null);
    city.add(new Option("洛阳市","洛阳市"),null);
   break;
    …… 
  }
Copy after login

3、属性:

options[] 返回包含下拉列表中的所有选项的一个数组
selectedIndex 设置或返回下拉列表中被选项目的索引号
length 返回下拉列表中的选项的数目

示例:


 function get(){
   var index=document.getElementById("fruit").selectedIndex;
   var len=document.getElementById("fruit").length;
   var show=document.getElementById("show");
   show.innerHTML="被选选项的索引号为:"+index+"<br/>下拉列表选项数目为:"+len;
  }
Copy after login

4、Option对象常用属性:

text:设置或返回某个选项的纯文本值
value:设置或返回被送往服务器的值

10、数组常用的属性和方法。

属性 length 设置或返回数组中元素的数目

方法:

join( ) 把数组的所有元素放入一个字符串,通过一个的分隔符进行分隔
sort( ) 对数组的元素进行排序

****读取二维数组中的元素值:


var cityList = new Array();
 cityList[&#39;河北省&#39;] = [&#39;邯郸市&#39;,&#39;石家庄市&#39;];
 cityList[&#39;河南省&#39;] = [&#39;郑州市&#39;,&#39;洛阳市&#39;];
 cityList[&#39;湖北省&#39;] = [&#39;武汉市&#39;,&#39;宜昌市&#39;];
 for(var i in cityList){
  document.getElementById("show").innerHTML+=i+"<br/>"; 
 }
  for(var j in cityList){
  for(var k in cityList[j]){
  document.getElementById("show").innerHTML+=cityList[j][k]+"  ”;
 }
 document.getElementById("show").innerHTML+="<br/>“; }
Copy after login

The above is the detailed content of Detailed explanation of JavaScript regular expressions and cascading effects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template