*******高分求一js正则
原始字符串:p1m1h1t1o1d1c1z1x1n1u1g1/
要求:
上面的url字符串中如果包含有"m数字"的话, 就替换成自己指定的"m数字"
比如:
输入m2, 字串变成:p1m2h1t1o1d1c1z1x1n1u1g1/
输入h4,字串变成p1m1h4t1o1d1c1z1x1n1u1g1/
输入e2, 字符串不变(因为原始字符串中没有e开头的字母)
------解决方案--------------------<!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=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function change(a){
var str="p1m1h1t1o1d1c1z1x1n1u1g1/";
var context=a.value;
var t=context.substring(0,1);
var reg=new RegExp(t+"\\d","g");
str=str.replace(reg,context);
alert(str);
}
</script>
</head>
<body>
<input type="text" onblur="change(this)">
</body>
</html>
这样试试
------解决方案--------------------
var s = 'p1m1h1t1o1d1c1z1x1n1u1g1aa1bb12/', reg,
tag = ['p0', 'm0', 'aa01', 'bb00'], x;
for(var i = 0, il = tag.length; i < il; i++){
x = tag[i].replace(/\d+/, '');
reg = new RegExp(x+'\\d+', 'g');
alert('替换 前:\t' + s + '\r\n替换目标:\t' + tag[i] + '\r\n替换结果:' + s.replace(reg,tag[i]));
}
------解决方案--------------------var pattern=/m\d+/g;
------解决方案--------------------
var s = "p1m1h1t1o1d1c1z1x1n1u1g1";
function regObj( g, s ){
var letter = g.substring(0,1);
alert( s.replace( eval("/"+letter+"\\d/g") , g ) );
}
regObj( "e2" , s );