<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var str = " 取自花丛懒回首,半缘修道半缘君 "
var str1= str.replace(/[半][缘君]*/g,function(kk){//kk指符合前面正则的参数
var num = "";
for(var i = 0; i<kk.length; i++){
num =num + "*";
}
return num;
})
console.log(str1);//取自花丛懒回首,**修道***
var str2 = str.replace(/^\s/,"");
console.log(str2);//取自花丛懒回首,半缘修道半缘君
</script>
</body>
</html>
关于字符串API replace的巧用
最新推荐文章于 2022-01-06 15:53:12 发布