Home  >  Article  >  Web Front-end  >  Summary of special symbols in regular expressions and several methods of regular expressions (replace, test, search)_javascript skills

Summary of special symbols in regular expressions and several methods of regular expressions (replace, test, search)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:12:401381browse

一 正则表达式的正文。
正则表达式中使用了特殊符号。下面我就将各种符号以及其意义和用法简单的介绍一下(说明:"X以上包括X"):

/ 表示在其后的文字是特殊符号。例:"n"和"n"是一致的。"/n"和换行符是一致的。
^ 和输入的开始一致。
$ 和输入的结尾一致。
* 与此符号之前的文字0次以上相同的话,两者一致。例:"zo*"和"zoo","z"都一致。
+ 与此符号之前的文字1次以上相同的话,两者一致。例:"zo*"和"zoo"一致,但和"z"不一致。
? 与此符号之前的文字0次或1次相同的话,两者一致。例:"a?ve?"和"never"的"ve"一致。
. 与除了换行符的所有单一文字一致。
(正则表达式) 以所指定的表达式寻找一致文字。如果找到了,储存下来。一致的部分可以从
Match方法获得的阵列中找到。
x|y x和y的任意一方相同都会被认为一致。例:"(z|f)ood"和"zood","food"都一致。
{n} n是0以上的整数。与其前的文字n次相同的话,两者一致。例:"o{2}"和"Bob"中的"o"不一
致,与"foooood"中的前两个"o"一致。
{n,} n是0以上的整数。与其之前的文字至少n次相同才一致。
{n,m} 两者整数。n至m范围的次数一致。
[xyz] 与中括号中的文字任意一个相同都被认为一致。
[^xyz] 与上面的相反。
[a-z] 文字的范围,从"a"到"z"的文字都被认为一致。
[^a-z] 与上面的相反。
/b 表示单词的末尾。例:"er/b"和"never"的"er"一致,但和"verb"的"er"不一致。
/B 表示非单词的末尾。
/d 表示数字。
/D 表示非数字。
/s 表示空格。
/S 表示非空格。
/w 表示所有字母数字。
/W 表示非所有字母数字。

   i   (忽略大小写)  
  g   (全文查找出现的所有   pattern)  
  gi   (全文查找、忽略大小写)
/num num应该被赋予一个正数。与已经储存的部分比较。例:"(.)/1"和任意的两个连续的相同
文字一致。

二 如何定义一句正文:
方法一:直接写
var s=/正则表达式/i或g或ig

方法二:建立对象实例:
var s=new RegExp(正则表达式,i或g或ig)


三 与正则表达式有关的方法:

1 exec方法
描述: 在制定的文字行内进行搜索。
结构: 正则表达式.exec(字符串)。
解说: 检索的返回:
null 没有检索到;
一致的结果 检索到后;
例:
代码片段如下:
<script> <br>var s='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp' <br>var r=new RegExp('g','i'); <br>var a=r.exec(s); <br>alert(a); <br></script>

2 compile方法:
描述: 修改正则表现的内部形式。
结构: 正则表达.compile('正文','g或i或ig')。
解说: 没什么好说的。
例:
代码片段如下:
<script> <br>var s='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp' <br>var r=new RegExp('[A-Z]','g'); <br>var a=s.match(r); <br>alert(a); <br>r.compile('[a-z]','g'); <br>var a=s.match(r); <br>alert(a); <br></script>


3 test方法:
描述: 顾名思义,做测验。
结构: 正则表达.test(字符串)。
解说: 返回:
false 没找到;
true 找到了;
例:
代码片段如下:
<script> <br>var re=/re/g; <br>var msg='return'; <br>var msg1='goon'; <br>alert(re.test(msg)); <br>alert(re.test(msg1)); <br></script>


4 replace方法:
描述: 寻找一致的并置换他。
结构: 字符串.replace(正则表达,替换字符串)。
解说: 与字符串不变化,返回其副本。
例:
代码片段如下:
<script> <br>var s='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp' <br>var r=new RegExp('[A-Z]','g'); <br>var a=s.replace(/[a-z]/g,'A'); <br>alert(a); <br></script>


5 match方法:
描述: 实行检索。
结构: 字符串.match(正则表达)。
解说: 返回数列。
例:
代码片段如下:
<script> <br>var re=/re/g; <br>var msg='rererere'; <br>var msg1='goon'; <br>alert(msg.match(re)); <br>alert(msg1.match(re)); <br></script>


6 split方法:
描述: 分割字符串。
结构: 字符串.split(正则表达)。
解说: 返回数列。
例:
代码片段如下:
<script> <br>var s="hello this good world"; <br>var p=//s/g; <br>var a=s.split(p); <br>alert(a); <br></script>


7 search method:
Description: Returns the position of a consistent string. (This is much more versatile than indexOf!)
Structure: String.search (regular expression).
Explanation: Returns
positive integer if found
-1 if not found
Example:
The code snippet is as follows:
<script> <br>var s="hello this good world"; <br>var p=/good/g; <br>var a=s.search(p); <br>alert(a); <br></script>


Change the example of the replace method:
The code snippet is as follows:
<script> <br>var s="hellOSCF"; <br>var r=new RegExp("[ A-Z]","g"); <br>s=s.replace(r,"a"); <br>alert(s) <br></script>


Finally, there are his various attributes

1 lastIndex attribute:
Description: Set the starting position of the search, and you can get its value
Structure: Regular expression.lastIndex(=value).
Explanation:
When lastIndex is greater than the search text length, if executed using the test or exec methods, the execution fails, and the
lastIndex attribute is set to 0.
When lastIndex is equal to the length of the retrieved text, if the expression body is empty, it is the same. Others In this case,
fails to execute and is reset to 0.
In cases other than the above, lastIndex will be set to the position pointer of the last consistent text string.

2 source attribute
Description: Returns the text of the regular expression
Structure: regular expression.source
Example:
The code snippet is as follows:
<script> <br>var s= /[a-z]{3}/W/s/g; <br>var s1=new RegExp("[a-z]{3}/W","g"); <br>alert(s.source); <br>alert(s1.source); <br></script>

I will write out several character processing functions below:

1 Numbers are strictly prohibited
The code snippet is as follows:
function check(msg){
var exe=//d/g;
if(exe.test(msg))return(0) ;
else return(1)
}

2 Only letters allowed
The code snippet is as follows:
function check(msg){
var exe=//W/g;
if(exe.test(msg))return(0 );
else return(1);
}


3 Strictly Prohibited Code
The code snippet is as follows:
function check(msg){
var exe=/<(/w|/W)*>/g;
if(exe.test(msg))return(0);
else return(1);

Statement:
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