日期:2014-05-16 浏览次数:20376 次
<html> <script language="JavaScript"> function test(){ var s1=document.getElementsByName("name")[0].value; var s2=document.getElementsByName("name2")[0].value; alert(s1.replace(/1\.1\.1\.1/,s2)); } </script> <input name="name" value="abc_1.1.1.1.exe" type="text"/> <input name="name2" value="2.3" type="text"/> <button onclick="test()">替换</button> </html>
------解决方案--------------------
<script type="text/javascript"> function foo(){ var o=document.getElementById("name1"); var t=document.getElementById("name2").value; o.value=o.value.replace(/[^_]+(?=\.exe|\.rar)/,t); } </script> <input name="name" value="abc_1.1.1.1.rar" type="text" id="name1"/> <input name="name2" value="2.3" type="text" id="name2"/> <button onclick="foo()">替换</button>
------解决方案--------------------
function replaceText() { var v1=document.getElementsByName("name")[0]; var v2=document.getElementsByName("name2")[0]; v1.value = v1.value.replace(/(\w*)_(.*)\.exe/mg, "$1_"+v2.value+".exe"); }
------解决方案--------------------
<html>
<script language="JavaScript">
function test(){
var s1=document.getElementsByName("name")[0].value;
var s2=document.getElementsByName("name2")[0].value;
var reg=/(\d\.)+(?=exe)/
alert(s1.replace(reg,s2+"."));
}
</script>
<input name="name" value="abc_1.1.1.1.exe" type="text"/>
<input name="name2" value="2.3" type="text"/>
<button onclick="test()">替换</button>
</html>