s = s.indexOf("text-indent") == -1?'<p>':'<p style = "text-indent: 2em;">';最针对性,又有点那啥的方法,就是直接判断就行了。var s = '<p asdfas style="line-height: 150%; tex1t-indent: 2em; mso-char-indent-count: 2.0">';
var reg = /\<p[^\>]*(\s+style=\"([^\"]*)\")[^\>]*\>/g;
s = s.replace(reg,function($1,$2,$3){
if($2.indexOf("text-indent") == -1){
return $1.replace($2,"");
}else{
return $1.replace($3,"text-indent: 2em;");
}
});
console.log(s);正则的话,这样就会只对style做处理,如果p中有其他的属性的话,就保留其他属性。试试看,这里是把目的当做字符串处理的。
如果你是要使用工具对文件中进行替换,把其中的转义字符的反斜杠去掉就可以使用了。试试行不。
var reg = /\<p[^\>]*(\s+style=\"([^\"]*)\")[^\>]*\>/g;
s = s.replace(reg,function($1,$2,$3){
if($2.indexOf("text-indent") == -1){
return $1.replace($2,"");
}else{
return $1.replace($3,"text-indent: 2em;");
}
});
console.log(s);正则的话,这样就会只对style做处理,如果p中有其他的属性的话,就保留其他属性。试试看,这里是把目的当做字符串处理的。
如果你是要使用工具对文件中进行替换,把其中的转义字符的反斜杠去掉就可以使用了。试试行不。
解决方案 »
- js如何去改变某个嵌套的iframe内容的文字大小
- 如何链接前先检测是否登录
- javascript 关于表格着色
- window.functionName = new Function() 有什么特别吗?
- 如何使用单层来显示图片?
- |zyciis| 我自己写了一个图片按比例缩放的,但是当网速不是很快的时候还是会看到图片把网页撑开 在怎么办 谢谢
- 如何获得web控件的位置?
- 关于画图的问题(百分相送)
- 我想在网页中动态显示数据库中的内容,请问我的代码有什么问题?总是提示没有Server!
- 迷茫中,请指点
- 浏览器扩展的content_scripts应该怎么写啊?怎么响应类似onClick这样的事件?
- 求救~ 哪位亲有这样的效果,求分享,感激不尽~
function replacep(str){
var reg=/^(<p style\b.*)(text-indent:[^;]+;)(.*>)$/g; if(reg.test(str)){
return str.replace(reg, "$1text-indent: 2em;$3");
}
else{
return "<p>";
}
}var canMatchedStr="<p style=\"line-height: 150%; text-indent: 2em; mso-char-indent-count: 2.0\">";
var cannotMatchedStr="<p style=''>";alert(replacep(canMatchedStr)); //<p style="line-height: 150%; text-indent: 3em; mso-char-indent-count: 2.0">
alert(replacep(cannotMatchedStr)); //<p>
<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">
window.onload=function(){
var ps=document.getElementsByTagName("p");
for(var i=0;i<ps.length;i++){
if(ps[i].style.textIndent){
ps[i].style.textIndent='2em';
}
}
}
</script>
</head><body>
<p style="line-height: 150%; text-indent: 2em; mso-char-indent-count: 2.0">123</p>
<p style="line-height: 150%; mso-char-indent-count: 2.0">456</p>
<p style="line-height: 150%; text-indent: 3em; mso-char-indent-count: 2.0">123</p>
</body>
</html>如果是字符串的话可以用正则 但如果是dom节点的话感觉操作dom方便点