请注意,我不知道我想隔离的完整类是什么,仅是其格式,例如:
class="loadable context-enc encounterTimelineEncounterDateitem"
我正在寻找的是context-enc或匹配的东西:
.match(/\bcontext-[a-z]+\b/)
这确实是我想发现的[az] +部分。我不想太多测试那里是否有一个匹配的类,但是想知道“ context-”之后的字符串是什么(在本例中为“ enc”)。就像是:
function getcontext(class, 'context-'){ .. }
以下功能可能会返回您想要的内容...
function getContext(className, withString){
var r = new RegExp("\\b"+ withString +"[a-z]+\\b");
var s = new RegExp("^"+withString);
return className.match(r)[0].replace(s,'');
}
如果您动态构建正则表达式,则可以将变量内容插入其中,然后要隔离该[a-z]+
部分,只需从结果中删除搜索到的字符串即可。
编辑:使用@ nathan-taylor的捕获组建议,我们可以将函数简化为:
function getContext(className, withString){
var r = new RegExp("\\b"+ withString +"([a-z]+)\\b");
return className.match(r)[1];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句