在下面的代码片段中,我不确定如何将正则表达式分配给 match(),是否始终需要正则表达式?:
----------匹配------------
case 'remove':
var rep=o.className.match(' '+c1)?' '+c1:c1;
o.className=o.className.replace(rep,'');
break;
------完整--代码--------------
function cssjs(a,o,c1,c2){
switch (a){
case 'swap':
if(!domtab.cssjs('check',o,c1)){
o.className.replace(c2,c1)
}else{
o.className.replace(c1,c2);
}
break;
case 'add':
if(!domtab.cssjs('check',o,c1)){
o.className+=o.className?' '+c1:c1;
}
break;
case 'remove':
var rep=o.className.match(' '+c1)?' '+c1:c1;
o.className=o.className.replace(rep,'');
break;
case 'check':
var found=false;
var temparray=o.className.split(' ');
for(var i=0;i<temparray.length;i++){
if(temparray[i]==c1){found=true;}
}
最佳答案
将字符串传递给 match
与将 new RegExp(s)
的结果传递给它是一样的(其中 s
是字符串)。例如,它们执行完全相同的操作:
a = str.match("foo");
a = str.match(new RegExp("foo"));
来自the standard :
- If
Type(regexp)
isObject
and the value of the[[Class]]
internal property of regexp is "RegExp", then letrx
beregexp
;- Else, let
rx
be a newRegExp
object created as if by the expressionnew RegExp( regexp)
whereRegExp
is the standard built-in constructor with that name.
关于javascript - 不带 RegEX 的 String.Match(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7946228/