RegExp for connecthen to camelCase? [英] RegExp for hyphen to camelCase?

查看:67
本文介绍了RegExp for connecthen to camelCase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在搞乱getPropertyValue(Mozilla等)和currentStyle(IE)

并且有一般功能(稍微修改一个原来的

由Steve van Dongen发布)获取样式属性:


函数GetCurrentStyle(el,prop){

if(window.getComputedStyle) {

// Mozilla等人

返回window.getComputedStyle(el,''')。getPropertyValue(prop));

} // IE5 +

else if(el.currentStyle){

返回el.currentStyle [prop];

} // IE4

else if(el.style){

返回el.style [prop];

}

}


如果属性名称没有连字符(例如宽度,高度)那么一切都很好。

但是带有带连字符的名称(例如margin-right,border-top)Mozilla

希望它们不被修改,IE希望它们可以使用camelCased(marginRight,borderTop)。


是否有一个简单的RegExp将使用camelCase连字符字符串?我可以使用循环和字符串操作来实现
但是如果可能的话,我更愿意使用单个

RegExp。


以下工作正常使用子字符串(对于具有0或更多

连字符的s)但有更简单的方法吗?


函数toCamel(s){

s = s.split('' - '');

var i = 0,j = s.length;

while(++ i< ; j){

s [0] + = s [i] .substring(0,1).toUpperCase()+ s [i] .substring(1);

}

返回s [0];

}


我尝试过使用RegExp但是无法获得一个简单的表达式没有

循环 - 属性名称最多3个(或更多?三个d-light-shadow)

连字符,带有全局标志的RegExp应该是可能的。


有什么建议吗?


-

Rob


I''m messing with getPropertyValue (Mozilla et al) and currentStyle (IE)
and have a general function (slightly modified from one originally
posted by Steve van Dongen) for getting style properties:

function GetCurrentStyle( el, prop ) {
if ( window.getComputedStyle ) {
// Mozilla et al
return window.getComputedStyle(el, '''').getPropertyValue(prop) );
} // IE5+
else if ( el.currentStyle ) {
return el.currentStyle[prop];
} // IE4
else if ( el.style ) {
return el.style[prop];
}
}

If property names have no hyphen (e.g. width, height) then all is fine.
But with hyphenated names (e.g. margin-right, border-top) Mozilla
wants them unmodified and IE wants them camelCased (marginRight, borderTop).

Is there a simple RegExp that will camelCase hyphenated strings? I can
do it with a loop and string operations but would prefer to use a single
RegExp if possible.

The following works fine using substring (for s having 0 or more
hyphens) but is there a simpler way?

function toCamel ( s ) {
s = s.split(''-'');
var i=0, j=s.length;
while ( ++i < j ) {
s[0] += s[i].substring(0,1).toUpperCase() + s[i].substring(1);
}
return s[0];
}

I''ve tried using RegExp but can''t get a simple expression without
looping - property names have up to 3 (or more? three-d-light-shadow)
hyphens, a RegExp with global flag should be possible.

Any suggestions?

--
Rob

推荐答案




RobG写道:


RobG wrote:
我搞乱了getPropertyValue(墨子lla et al)和currentStyle(IE)
并且具有一般功能(稍微修改自Steve van Dongen最初发布的一个)以获取样式属性:

函数GetCurrentStyle( el,prop){
if(window.getComputedStyle){
// Mozilla et al
return window.getComputedStyle(el,'''')。getPropertyValue(prop));
} // IE5 +
否则如果(el.currentStyle){
返回el.currentStyle [prop];
} // IE4
else if(el.style){
返回el.style [prop];
}


如果属性名称没有连字符(例如宽度,高度)然后一切都很好。
但是带有连字符的名称(例如margin-right,border-top)Mozilla
希望它们不被修改,IE希望它们是camelCased(marginRight,borderTop)。

是否有一个简单的RegExp将使用带连字符的字符串?我可以使用循环和字符串操作来执行此操作,但如果可能的话,我更愿意使用单个
RegExp。

以下使用子字符串工作正常(对于具有0或更多的s)
连字符)但有更简单的方法吗?

功能toCamel(s){
s = s.split('' - '');
var i = 0,j = s.length;
while(++ i< j){
s [0] + = s [i] .substring(0,1).toUpperCase()+ s [i] .substring(1);
}
返回s [0];
}
我尝试过使用RegExp但是无法获得一个简单的表达式没有
循环 - 属性名称最多有3个(或更多?三个d-light-shadow)连字符,一个带有全局标志的RegExp应该是可能的。
有什么建议吗?

-
I''m messing with getPropertyValue (Mozilla et al) and currentStyle (IE)
and have a general function (slightly modified from one originally
posted by Steve van Dongen) for getting style properties:

function GetCurrentStyle( el, prop ) {
if ( window.getComputedStyle ) {
// Mozilla et al
return window.getComputedStyle(el, '''').getPropertyValue(prop) );
} // IE5+
else if ( el.currentStyle ) {
return el.currentStyle[prop];
} // IE4
else if ( el.style ) {
return el.style[prop];
}
}

If property names have no hyphen (e.g. width, height) then all is fine.
But with hyphenated names (e.g. margin-right, border-top) Mozilla
wants them unmodified and IE wants them camelCased (marginRight, borderTop).

Is there a simple RegExp that will camelCase hyphenated strings? I can
do it with a loop and string operations but would prefer to use a single
RegExp if possible.

The following works fine using substring (for s having 0 or more
hyphens) but is there a simpler way?

function toCamel ( s ) {
s = s.split(''-'');
var i=0, j=s.length;
while ( ++i < j ) {
s[0] += s[i].substring(0,1).toUpperCase() + s[i].substring(1);
}
return s[0];
}

I''ve tried using RegExp but can''t get a simple expression without
looping - property names have up to 3 (or more? three-d-light-shadow)
hyphens, a RegExp with global flag should be possible.

Any suggestions?

--



模式搜索很简单:


var re = /( - )([az])/ g;


hyphenString.replace(re,"


The pattern search would be simple:

var re = /(-)([a-z])/g;

hyphenString.replace(re,"


2");


这将改变ab-c到了abc


不幸的是回到NN4他们忘了包含在

JavaScript'的RegExp案例转换序列(\ u和\\ \\U)。并且

到目前为止没有人费心去解决它。所以没有办法改变

a-b-c到aBC除了使用.exec方法之外,通过所有

匹配并且每次都使用字符串重新组合来应用toUpperCase()。


在这种情况下旧的charAt / substring方法*多*更多时间/资源

有效:-(

2");

This would transform "a-b-c" to "abc"

Unfortunately back to NN4 they have forgotten to include in
JavaScript''s RegExp the case transformation sequences (\u and \U). And
so far no one bothered to fix it. So there is no way to transform
"a-b-c" to "aBC" other than using .exec method, pass through all
matches and apply toUpperCase() with string re-assemply each time.

In this case the old charAt/substring method *much* more time/ressource
effective :-(


我同意VK的评论。


根据你的评论你至少可以尝试这个: -


var s =" three-d-light-shadow" ;;

var r = /( - )([az])/ g;

s = s.replace(r,function(a,b,c){return c.toUpperCase();});

alert(s);

I would agree with VK''s comments.

Taking from both your comments you could try this at least:-

var s="three-d-light-shadow";
var r=/(-)([a-z])/g;
s=s.replace(r,function(a,b,c){return c.toUpperCase();});
alert(s);


这篇关于RegExp for connecthen to camelCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆