Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of js regular expressions_javascript skills

WBOY
Release: 2016-05-16 17:29:17
Original
1056 people have browsed it

Regular expressions in js are much weaker than regular expressions in C#, but they are basically enough
1 Define regular expressions
2 About verification Three expression methods
3 Escape characters of regular expressions

1 Define regular expressions
It is very easy to define regular expressions in js Simple, there are two ways, one is through the constructor, the other is through //, which is two slashes.
For example

Copy code The code is as follows:

var re =new RegExp("\ ?(\w{1,}=\w{1,}&){1,}\w{1,}=\w{1,}");

Use constructor definition Regular expressions, pay attention to capitalization, otherwise they will not work. Since the parameter of the constructor is a string, it can also be defined with two slashes. When encountering some special characters, you need to use escape
to define the same regular expression with double slashes
Copy code The code is as follows:

var re =/?(w{1,}=w{1 ,}&){1,}w{1,}=w{1,}/;

Copy code The code is as follows:

var re =new RegExp( /^?(w{1,}=w{1,}&){1,}w{1,}=w{ 1,}/);

can achieve the same effect as the constructor, but careful analysis shows that more escape characters are needed through the constructor

2 Three regular expression methods for verification

The main ones that use regular expressions are the string method match, the regular expression method exec, and test
The regular expression method test tests a given string Whether the regular expression is satisfied or not, the return value is of bool type, only true and false. If it is just a simple judgment, no other processing is required, and it can be used, especially for verification.
Copy code The code is as follows:

function test(){
var text=" index.aspx?test=1&ww=2&www=3"; // 
 var re =/?(w{1,}=w{1,}&){1,}w{1,}=w{1 ,}/;
// var re =new RegExp("\?(\w{1,}=\w{1,}&){1,}\w{1,}=\w{1, }");
var result= re.test(text);
if(result)
{
alert("ok");
}else
{
alert("err");
} }

}

Regular expression method exec tests whether the given string satisfies the regular expression and returns the matched string , if there is no match, return null, which is basically the same as test. If you need to get each matching substring, you can use subscripts. The above test example can be rewritten as follows
Copy code The code is as follows:

function test(){
var text="index.aspx?test=1&ww=2&www=3 ";
              var   re = /?(w{1,}=w{1,}&){1,}w{1,}=w{1,}/;                                                                                                                                                        ; RegExp( "\?(\w{1,}=\w{1,}&){1,}\w{1,}=\w{1,}");
var result= re.exec (text);
if(result)
{
alert("ok");
             alert(result[0]          0");//Yes?test=1&ww=2&www=3
                                                                                                                                                                                                                                   ;
{
alert("err");
}

}


match は実際には文字列メソッドですが、パラメーターは確かに正規表現です。上記の例を書き直すと、次のようになります。
コードをコピー コードは次のとおりです。

function test(){
var text="index.aspx?test=1&ww=234"; //
var re = /?( w{1,}=w{1,}&){1,}w{1,}=w{1,}/;
1,}&){1,}\w{1 ,}=\w{1,}"
var result= text.match(re);
if(result)
alert(result);//?test=1&ww=234,test=1&正規表現、分割、検索、置換などを渡すことができる関数は複数ありますが、これらのメソッドは検証には適していません。




コードをコピー


コードは次のとおりです。

function test(){
var text="index.aspx?test=1&ww=234"; //
var re = /?(w{1,}=w{1,}&){1,}w{1,}=w{ 1,} /; // var re2 = "(\w{1,}=\w{1,}&){1,}\w{1,}=\w{1,}" var result = text.split(re); } 3 通常のエスケープ文字式
エスケープ文字は、疑問符などの正規表現によく使用されます。正規表現内の特殊文字です。つまり、疑問符を一致させる必要がある場合は、エスケープ文字のバックスラッシュ
を使用する必要があります。次の両方は、疑問符



コードをコピー


コードは次のとおりです。

function test(){
var text="? test=1&ww=2&www=3"; var re = /^?(w{1,}=w{1,}&){1,}w{1,}=w{1,}/;/ / ?設定の疑問符を示します?
// var re =new RegExp( "^\?(\w{1,}=\w{1,}&){1,}\w{1,}=\ w{1,}");// \? は構成の疑問符を表します?
var result= re.exec(text);
if(result)
{
alter("ok" ); アラート(結果); アラート(結果[0] ",0"); }else { アラート("エラー"); } }
}



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