2011-02-23 45 views

回答

3
function getMatch(str) { 
    var m = /^cn=(.*?),ou=(.*?),o=(.*?),o=swift$/.exec(str); 
    return (m) ? {cn:m[1], ou:m[2], o:m[3]} : null; 
} 

如果給定的字符串不匹配,則返回null,否則返回值爲「cn」,「ou」和「o」的對象。

var x = getMatch("cn=foo,ou=bar,o=zip,o=swift"); 
alert(x.cn); // => "foo" 
alert(x.ou); // => "bar" 
alert(x.o); // => "zip" 
0
var regexp = /^[a-z]{1,2}[=]{1}[a-z0-9]+/; 
+0

這將只匹配第一次出現的令牌。 – 2011-02-23 09:21:38

+0

您顯然從未受過[x509](http://tools.ietf.org/html/rfc1422)或[ldap](http://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol#Directory_structure)的限制。 – sarnold 2011-02-23 09:24:31

相關問題