Created
June 15, 2015 05:25
-
-
Save dvdbng/4db30f4e3f4c9ad69d5b to your computer and use it in GitHub Desktop.
Find regexp of valid attribute names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var valid_first = []; | |
var valid_second = []; | |
for (var i = 0, len = 0xFFFF; i < len; i++) { | |
try{ | |
var char = String.fromCharCode(i); | |
if(char.toLowerCase() != char.toUpperCase() && valid_second.indexOf(char.toLowerCase().charCodeAt(0)) >= 0){ | |
continue; | |
} | |
document.body.setAttribute('a' + char, ''); | |
document.body.removeAttribute('a' + char); | |
valid_second.push(i); | |
document.body.setAttribute(char, ''); | |
document.body.removeAttribute(char, ''); | |
valid_first.push(i); | |
}catch(e){} | |
} | |
function charRepr(char){ | |
var str = String.fromCharCode(char); | |
if(char < 0xFF || str.toLowerCase() !== str.toUpperCase()){ | |
if(str == '.' || str == '-' || str == ']') { | |
return '\\' + str; | |
} | |
return str; | |
} else { | |
return '\\u' + ('00' + Number(char).toString(16).toUpperCase()).substr(-4); | |
} | |
} | |
function makeRanges(list) { | |
var ranges = []; | |
var rangeStart = list[0]; | |
var currentRangeLength = 1; | |
for (var i = 1, len = list.length; i < len; i++) { | |
if(list[i] !== rangeStart + currentRangeLength) { | |
if(currentRangeLength === 1){ | |
ranges.push(charRepr(rangeStart)); | |
} else { | |
ranges.push(charRepr(rangeStart) + '-' + charRepr(list[i-1])); | |
} | |
currentRangeLength = 1; | |
rangeStart = list[i]; | |
} else { | |
currentRangeLength++; | |
} | |
} | |
return ranges.join(''); | |
} | |
var ranges_start = makeRanges(valid_first); | |
var ranges_cont = makeRanges(valid_second); | |
var regexp_source = '[' + ranges_start + '][' + ranges_cont + ']*'; | |
console.log(regexp_source); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment