string.replace var output
Hi could some one show me how to d开发者_运维技巧o the following:
var regep = /margin-bottom:([^;]+); margin-left:0px; margin-right:0px; margin-top:([^;]+);/;
elementCSS = elementCSS.replace( regep , "margin-bottom:\\1; margin-left:auto; margin-right:auto; margin-top:\\2;");
\1 to = ([^;]+) (margin-bottom) and \2 to = ([^;]+) (margin-top)
?? Cant seem to figure it out..
Regards Phil
If you are trying to dynamically adjust the top and bottom margins, it would be easier to just manipulate the element's style attributes directly with
element.style.marginTop = x;
element.style.marginBottom = y;
I think you need to try using the Javascript RegExp.
- JavaScript RegExp Example: Regular Expression Tester
- Regular Expressions patterns
var regep = /margin-bottom:([^;]+); margin-left:0px; margin-right:0px; margin-
top:([^;]+);/;
elementCSS = elementCSS.replace( regep , "margin-bottom:$1; margin-left:auto; margin-right:auto; margin-top:$2;");
精彩评论