可以直接粘贴到页面就可以直接看效果了.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
body,td,th {
font-size: 14px;
}
.STYLE1 {
font-size: 16px;
font-weight: bold;
}
-->
</style></head>
<body>
<form name="form1" action="" method="get">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td colspan="5" scope="col"><span class="STYLE1">match 方法:</span> 对字符串执行查找,并将包含查找的结果作为数组返回</td>
</tr>
<tr>
<td>被查找字符串:</td>
<td><textarea name="matchchar" cols="40" rows="3">The rain in Spain falls mainly in the plain</textarea></td>
<td>需要查找字符串:</td>
<td><input type="text" name="matchval" value="\b\w{2}ain"></td>
<td valign="bottom"><input type="button" value="Match方法" onClick="MatchDemo()"></td>
</tr>
<script>
function MatchDemo()
{
var text1 = form1.matchchar.value;
var text2 = form1.matchval.value;
var res = new RegExp(text2,"i"); //如果加入g 则会全部查找,否则只找出第一次出现的值。
var r = text1.match(res);
alert(r);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">test 方法:</span> 指出在被查找的字符串中是否匹配表达式,
即:2个数字后跟一个字符或数字或下划线,再跟上3个数字</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="testchar" cols="40" rows="3">10_100</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="test方法" onClick="testDemo()"></td>
</tr>
<script>
function testDemo()
{
var text = form1.testchar.value;
var reg=/\d{2}\w\d{3}/i;
alert(reg.test(text));
}</script>
<tr>
<td colspan="5"><span class="STYLE1">split 方法:</span> 以空格分割字符串,并以数组返回,返回结果以逗号分割</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="splitchar" cols="40" rows="3" id="splitchar">The rain in Spain falls mainly in the plain</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="split方法" onClick="splitDemo()"></td>
</tr>
<script>
function splitDemo()
{
var text = form1.splitchar.value;
var texts = text.split(" ");
alert(texts);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">exec 方法:</span>在字符串中运行查找,并返回包含该查找结果的一个数组</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="execchar" cols="40" rows="3" id="execchar">The rain in Spain falls mainly in the plain</textarea></td>
<td>要查找字符串:</td>
<td><input type="text" name="execval" value="\w+"></td>
<td><input type="button" value="exec方法" onClick="execDemo()"></td>
</tr>
<script>
function execDemo(){
var text1 = form1.execchar.value;
var text2 = form1.execval.value;
var re = new RegExp(text2,"g"); //如果单独用i就会变成死循环
var arr;
var res="";
while ((arr = re.exec(text1)) != null)
res+=arr.index + "-" + arr.lastIndex + "\t" + arr+"\n";
alert(res);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">CompileDemo 方法:</span>分出大小写,返回结果以逗号分割</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="comchar" cols="40" rows="3" id="comchar">AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="compile方法" onClick="CompileDemo()"></td>
</tr>
<script>
function CompileDemo(){
var rs;
var s = form1.comchar.value;
// 只为大写字母创建正则表达式。
var r = new RegExp("[A-Z]", "g");
var a1 = s.match(r) // 查找匹配。
// 只为小写字母编译正则表达式。
r.compile("[a-z]", "g");
var a2 = s.match(r) // 查找匹配。
alert(a1 + "\n" + a2);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">Replace 方法:</span>文本替换方法一:指定替换单词,将被替换单词里的the替换为:A,
方法二:交换单词位置</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="repchar" cols="40" rows="3" id="repchar">The rain in Spain falls mainly in the plain</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="方法一" onClick="ReplaceDemo()">
<input type="button" value="方法二" onClick="ReplaceDemo2()">
<input type="button" value="三" onClick="Replace()"></td>
</tr>
<script>
function ReplaceDemo(){
var text1 = form1.repchar.value;
var re = /the/g; //如果加i则不区分大小写
var r = text1.replace(re,"A");
alert(text1+"\n"+r);
}
function ReplaceDemo2(){
var ss = form1.repchar.value;
var re = /(\S+)(\s+)(\S+)/g; // 创建正则表达式模式。
var r = ss.replace(re, "$3$2$1"); // 交换每一对单词。
alert(ss+"\n"+r);
}
function Replace()
{
var ss = "100000000";
var reg = /\d{1}/g;
var arr;
var i = 0;
var res="";
while((arr = reg.exec(ss)) != null)
{
if(i%3==0 && i!=0)
res+=",";
res += arr;
i++;
}
}</script>
</table>
</form>
</body>
</html>
如果是用JAVA 则
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int i=0;
String reg = "";
Pattern myre = Pattern.compile("\\d{1}");
Matcher matc = myre.matcher("1000000");
while(matc.find())
{
if(i%3==0 && i!=0)
reg += ",";
reg+=matc.group();
i++;
}System.out.println(reg);
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
body,td,th {
font-size: 14px;
}
.STYLE1 {
font-size: 16px;
font-weight: bold;
}
-->
</style></head>
<body>
<form name="form1" action="" method="get">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td colspan="5" scope="col"><span class="STYLE1">match 方法:</span> 对字符串执行查找,并将包含查找的结果作为数组返回</td>
</tr>
<tr>
<td>被查找字符串:</td>
<td><textarea name="matchchar" cols="40" rows="3">The rain in Spain falls mainly in the plain</textarea></td>
<td>需要查找字符串:</td>
<td><input type="text" name="matchval" value="\b\w{2}ain"></td>
<td valign="bottom"><input type="button" value="Match方法" onClick="MatchDemo()"></td>
</tr>
<script>
function MatchDemo()
{
var text1 = form1.matchchar.value;
var text2 = form1.matchval.value;
var res = new RegExp(text2,"i"); //如果加入g 则会全部查找,否则只找出第一次出现的值。
var r = text1.match(res);
alert(r);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">test 方法:</span> 指出在被查找的字符串中是否匹配表达式,
即:2个数字后跟一个字符或数字或下划线,再跟上3个数字</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="testchar" cols="40" rows="3">10_100</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="test方法" onClick="testDemo()"></td>
</tr>
<script>
function testDemo()
{
var text = form1.testchar.value;
var reg=/\d{2}\w\d{3}/i;
alert(reg.test(text));
}</script>
<tr>
<td colspan="5"><span class="STYLE1">split 方法:</span> 以空格分割字符串,并以数组返回,返回结果以逗号分割</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="splitchar" cols="40" rows="3" id="splitchar">The rain in Spain falls mainly in the plain</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="split方法" onClick="splitDemo()"></td>
</tr>
<script>
function splitDemo()
{
var text = form1.splitchar.value;
var texts = text.split(" ");
alert(texts);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">exec 方法:</span>在字符串中运行查找,并返回包含该查找结果的一个数组</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="execchar" cols="40" rows="3" id="execchar">The rain in Spain falls mainly in the plain</textarea></td>
<td>要查找字符串:</td>
<td><input type="text" name="execval" value="\w+"></td>
<td><input type="button" value="exec方法" onClick="execDemo()"></td>
</tr>
<script>
function execDemo(){
var text1 = form1.execchar.value;
var text2 = form1.execval.value;
var re = new RegExp(text2,"g"); //如果单独用i就会变成死循环
var arr;
var res="";
while ((arr = re.exec(text1)) != null)
res+=arr.index + "-" + arr.lastIndex + "\t" + arr+"\n";
alert(res);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">CompileDemo 方法:</span>分出大小写,返回结果以逗号分割</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="comchar" cols="40" rows="3" id="comchar">AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="compile方法" onClick="CompileDemo()"></td>
</tr>
<script>
function CompileDemo(){
var rs;
var s = form1.comchar.value;
// 只为大写字母创建正则表达式。
var r = new RegExp("[A-Z]", "g");
var a1 = s.match(r) // 查找匹配。
// 只为小写字母编译正则表达式。
r.compile("[a-z]", "g");
var a2 = s.match(r) // 查找匹配。
alert(a1 + "\n" + a2);
}</script>
<tr>
<td colspan="5"><span class="STYLE1">Replace 方法:</span>文本替换方法一:指定替换单词,将被替换单词里的the替换为:A,
方法二:交换单词位置</td>
</tr>
<tr>
<td>被匹配字符串:</td>
<td><textarea name="repchar" cols="40" rows="3" id="repchar">The rain in Spain falls mainly in the plain</textarea></td>
<td> </td>
<td> </td>
<td><input type="button" value="方法一" onClick="ReplaceDemo()">
<input type="button" value="方法二" onClick="ReplaceDemo2()">
<input type="button" value="三" onClick="Replace()"></td>
</tr>
<script>
function ReplaceDemo(){
var text1 = form1.repchar.value;
var re = /the/g; //如果加i则不区分大小写
var r = text1.replace(re,"A");
alert(text1+"\n"+r);
}
function ReplaceDemo2(){
var ss = form1.repchar.value;
var re = /(\S+)(\s+)(\S+)/g; // 创建正则表达式模式。
var r = ss.replace(re, "$3$2$1"); // 交换每一对单词。
alert(ss+"\n"+r);
}
function Replace()
{
var ss = "100000000";
var reg = /\d{1}/g;
var arr;
var i = 0;
var res="";
while((arr = reg.exec(ss)) != null)
{
if(i%3==0 && i!=0)
res+=",";
res += arr;
i++;
}
}</script>
</table>
</form>
</body>
</html>
如果是用JAVA 则
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
int i=0;
String reg = "";
Pattern myre = Pattern.compile("\\d{1}");
Matcher matc = myre.matcher("1000000");
while(matc.find())
{
if(i%3==0 && i!=0)
reg += ",";
reg+=matc.group();
i++;
}System.out.println(reg);
}
}