随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


search : 找到返回位置; 找不到返回 -1
var str, p, n;
str = '1:abc;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC';

p = /abc/i;
n = str.search(p);
alert(n); //2

p = /abcde/i;
n = str.search(p);
alert(n); //-1

//这个函数和 RegExp.test 差不多, 不过 test 返回的是 true/false; 全局选项 g 对它们都没有意义.


match : 匹配并返回结果, 结果是数组
//首先, String.match 和 RegExp.exec 功能类似, 但 match 更方便使用.

var str, p, arr;
str = '1:abc;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC';

//不指定全局匹配, 只返回第一组(包含子匹配)
p = /(ab)(c)/i;
arr = str.match(p);
alert(arr); //abc,ab,c

//指定了全局匹配, 会返回所有匹配结果(但不包括子匹配)
p = /(ab)(c)/ig;
arr = str.match(p);
alert(arr); //abc,Abc,aBc,abC,ABc,aBC,AbC,ABC

//这个使用虽然方便了, 但不能获取所有子匹配的细节; 但 exec 可以.


replace : 强大的替换函数; replace(正则表达式, 要替换的内容); 返回替换后的结果
var str1, str2, p;
str1 = '1:abc;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC';
p = /abc/ig;
str2 = str1.replace(p, '*');
alert(str2); //1:*;2:*;3:*;4:*;5:*;6:*;7:*;8:* 

p = /abc/i;
str2 = str1.replace(p, '*');
alert(str2); //1:*;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC 

//$1...$99 表示子表达式:
var str1, str2, p;
str1 = '1:abc;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC';
p = /(ab)(c)/ig;
str2 = str1.replace(p, '$1');
alert(str2); //1:ab;2:Ab;3:aB;4:ab;5:AB;6:aB;7:Ab;8:AB

p = /(ab)(c)/ig;
str2 = str1.replace(p, '《$2》');
alert(str2); //1:《c》;2:《c》;3:《c》;4:《C》;5:《c》;6:《C》;7:《C》;8:《C》 

//$& 表示整个表达式的匹配结果
var str1, str2, p;
str1 = '1:abc;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC';
p = /(ab)(c)/ig;
str2 = str1.replace(p, '"$&"');
alert(str2); //1:"abc";2:"Abc";3:"aBc";4:"abC";5:"ABc";6:"aBC";7:"AbC";8:"ABC" 

//$` 表示匹配结果左边的内容; $' 表示匹配结果右边的内容; $ 自身得用 $$ 表示.
var str1, str2, p;
str1 = '1234ABC5678';
p = /ABC/;
str2 = str1.replace(p, '《$`》');
alert(str2); //1234《1234》5678 

str2 = str1.replace(p, "《$'》");
alert(str2); //1234《5678》5678 

str2 = str1.replace(p, '$$');
alert(str2); //1234$5678 

//replace 的第二个参数可以是个函数(注意函数的调用方式有点特别)
var str1, str2, p;

str1 = '1:abc;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC';
p = /(ab)(c)/ig;

function fun1(s) { return s.toUpperCase();}
function fun2(s) { return '《' + s.toLowerCase() + '》';}
function fun3(s) { return s.length;}

str2 = str1.replace(p, fun1);
alert(str2); //1:ABC;2:ABC;3:ABC;4:ABC;5:ABC;6:ABC;7:ABC;8:ABC 

str2 = str1.replace(p, fun2);
alert(str2); //1:《abc》;2:《abc》;3:《abc》;4:《abc》;5:《abc》;6:《abc》;7:《abc》;8:《abc》  

str2 = str1.replace(p, fun3);
alert(str2); //1:3;2:3;3:3;4:3;5:3;6:3;7:3;8:3 


split : 分割字符串, 返回数组
var str1, arr, p;
str1 = '1:abc;2:Abc;3:aBc;4:abC;5:ABc;6:aBC;7:AbC;8:ABC';

p = /abc/i;
arr = str1.split(p);
alert(arr); //1:,;2:,;3:,;4:,;5:,;6:,;7:,;8: 

p = /;/;
arr = str1.split(p);
alert(arr); //1:abc,2:Abc,3:aBc,4:abC,5:ABc,6:aBC,7:AbC,8:ABC

p = /;\d:/;
arr = str1.split(p);
alert(arr); //1:abc,Abc,aBc,abC,ABc,aBC,AbC,ABC


简单使用这几个函数
//如果在正则表达式的位置给了字符串, JavaScript 也会把它们转成正则表达式; 但不使用正则功能太弱了.
var str1, str2, n, arr;
str1 = 'Delphi 2009';

n = str1.search('p');
alert(n); //3

arr = str1.match('p');
alert(arr); //p

str2 = str1.replace('0', '*');
alert(str2); //Delphi 2*09

arr = str1.split(' ');
alert(arr[0]); //Delphi
alert(arr[1]); //2009


posted on 2009-03-05 21:20  万一  阅读(...)  评论(...编辑  收藏