Last active
January 4, 2018 19:19
-
-
Save luislobo14rap/d6ec6e8b34a740e3cd66f3ac08ff2e4d to your computer and use it in GitHub Desktop.
This file contains 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
String.prototype.htmlEntities = function( reverse = 0 ){ | |
let thisString = this; | |
let entities = [' ', '<', '>', '"', '\'', '¢', '£', '¥', '€', '©', '®']; | |
let htmlEntities = [' ', '<', '>', '"', ''', '¢', '£', '¥', '€', '©', '®']; | |
if( reverse == 0 ){ | |
thisString = thisString.replace(/&/g, 'ΠπϖⲠⲡhE'); | |
for(let i = 0; i < 11; i++){ | |
thisString = thisString.replace( new RegExp( entities[i] , 'g'), htmlEntities[i] ).replace(/ΠπϖⲠⲡhE/g, '&'); | |
}; | |
}else{ | |
entities.push('&'); | |
htmlEntities.push('&'); | |
for(let i = 0; i < 12; i++){ | |
thisString = thisString.replace( new RegExp( htmlEntities[i] , 'g'), entities[i] ); | |
}; | |
}; | |
return thisString; | |
}; | |
/* | |
EXAMPLE: | |
> '<span>hi</span>'.htmlEntities() | |
< '<span>hi</span>' | |
> '<span>hi</span>'.htmlEntities(1) //USE 1 FOR REVERSE | |
< '<span>hi</span>' | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment