Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active April 28, 2021 09:05
Show Gist options
  • Save luislobo14rap/8a74a4198bb68adaa4994b2ecf131a83 to your computer and use it in GitHub Desktop.
Save luislobo14rap/8a74a4198bb68adaa4994b2ecf131a83 to your computer and use it in GitHub Desktop.
to-simple-text.js
<!-- to-simple-text.js v1.2.3 e RegExp -->
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>bootstrap starter template</title>
<style>
p{
margin-top: 1rem;
}
.w-90{
width: 90%;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row no-gutters">
<div class="col-12 mx-auto text-center">
<textarea class="textarea-to-simple-text mx-auto w-90 mt-5 d-block" cols="30" rows="10"></textarea>
<p></p>
<button class="btn-to-simple-text-textarea btn btn-primary d-inline-block mx-auto mt-3">toSimpleText to textarea</button>
<button class="btn-approved btn btn-primary d-inline-block mx-auto mt-3">Approved characters</button>
<button class="btn-to-simple-text-p btn d-inline-block mx-auto mt-3">toSimpleText to p</button>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script>
$(function(){
$('.btn-to-simple-text-textarea').on('click', function(){
$('textarea').val( $('textarea').val().toSimpleText() );
});
$('.btn-to-simple-text-p').on('click', function(){
$('p').text( $('textarea').val().toSimpleText() );
});
$('.btn-approved').on('click', function(){
$('p').text('');
if( !!$('textarea').val().match(/[^( |a-z|A-Z|'|"|1|!|¹|2|@|²|3|#|³|4|\$|£|5|%|¢|6|¨|¬|7|&|8|\*|9|\(|0|\)|\-|_|=|\+|§|´|`|\[|{|ª|~|\^|\]|}|º|₢|,|<|\.|>|;|:|\||\\|\/|\?|°|Á|Â|Ã|Ä|Å|á|â|ã|ä|å|Ć|Ĉ|Ḉ|ć|ĉ|ḉ|É|Ê|Ë|é|ê|ë|Ĝ|ĝ|Í|Î|Ï|í|î|ï|Ǹ|Ń|ǹ|ń|Ó|Ô|Õ|Ö|ó|ô|õ|ö|Ŝ|Ś|ŝ|ś|Ú|Û|Ü|ú|û|ü|Ẁ|Ŵ|ẁ|ŵ|Ÿ|Ỳ|Ỹ|Ŷ|ÿ|ỳ|ỹ|ŷ|Ẑ|Ź|ẑ|ź|À|à|Ç|ç|È|è|Ǵ|ǵ|Ì|ì|Ñ|ñ|Ò|ò|Š|š|Ù|ù|Ẃ|ẃ|Ý|ý|Ž|ž|Ð|ð|Ĥ|ĥ|Ĵ|ĵ|Ḱ|ḱ|Ĺ|ĺ|Ḿ|ḿ|Ṕ|ṕ|Ŕ|ŕ|Ṽ|ṽ|\t|\n)]/gim) ){
let invalidList = $('textarea').val().match(/[^( |a-z|A-Z|'|"|1|!|¹|2|@|²|3|#|³|4|\$|£|5|%|¢|6|¨|¬|7|&|8|\*|9|\(|0|\)|\-|_|=|\+|§|´|`|\[|{|ª|~|\^|\]|}|º|₢|,|<|\.|>|;|:|\||\\|\/|\?|°|Á|Â|Ã|Ä|Å|á|â|ã|ä|å|Ć|Ĉ|Ḉ|ć|ĉ|ḉ|É|Ê|Ë|é|ê|ë|Ĝ|ĝ|Í|Î|Ï|í|î|ï|Ǹ|Ń|ǹ|ń|Ó|Ô|Õ|Ö|ó|ô|õ|ö|Ŝ|Ś|ŝ|ś|Ú|Û|Ü|ú|û|ü|Ẁ|Ŵ|ẁ|ŵ|Ÿ|Ỳ|Ỹ|Ŷ|ÿ|ỳ|ỹ|ŷ|Ẑ|Ź|ẑ|ź|À|à|Ç|ç|È|è|Ǵ|ǵ|Ì|ì|Ñ|ñ|Ò|ò|Š|š|Ù|ù|Ẃ|ẃ|Ý|ý|Ž|ž|Ð|ð|Ĥ|ĥ|Ĵ|ĵ|Ḱ|ḱ|Ĺ|ĺ|Ḿ|ḿ|Ṕ|ṕ|Ŕ|ŕ|Ṽ|ṽ|\t|\n)]/gim);
for(let i = 0; i < invalidList.length; i++){
console.log(invalidList[i]);
};
$('p').text( invalidList.join('') );
};
$('textarea').val( $('textarea').val().replace(/[^( |a-z|A-Z|'|"|1|!|¹|2|@|²|3|#|³|4|\$|£|5|%|¢|6|¨|¬|7|&|8|\*|9|\(|0|\)|\-|_|=|\+|§|´|`|\[|{|ª|~|\^|\]|}|º|₢|,|<|\.|>|;|:|\||\\|\/|\?|°|Á|Â|Ã|Ä|Å|á|â|ã|ä|å|Ć|Ĉ|Ḉ|ć|ĉ|ḉ|É|Ê|Ë|é|ê|ë|Ĝ|ĝ|Í|Î|Ï|í|î|ï|Ǹ|Ń|ǹ|ń|Ó|Ô|Õ|Ö|ó|ô|õ|ö|Ŝ|Ś|ŝ|ś|Ú|Û|Ü|ú|û|ü|Ẁ|Ŵ|ẁ|ŵ|Ÿ|Ỳ|Ỹ|Ŷ|ÿ|ỳ|ỹ|ŷ|Ẑ|Ź|ẑ|ź|À|à|Ç|ç|È|è|Ǵ|ǵ|Ì|ì|Ñ|ñ|Ò|ò|Š|š|Ù|ù|Ẃ|ẃ|Ý|ý|Ž|ž|Ð|ð|Ĥ|ĥ|Ĵ|ĵ|Ḱ|ḱ|Ĺ|ĺ|Ḿ|ḿ|Ṕ|ṕ|Ŕ|ŕ|Ṽ|ṽ|\t|\n)]/gim, '') );
});
// to-simple-text.js v1.2.3
function toSimpleText(text){
if( text == undefined || text == null ){
return '';
};
text = text.toString();
text = text.replace(/[\s\uFEFF\xA0]+/g, ' ').replace(/(^ +| +$)/g, '');
return text;
};
String.prototype.toSimpleText = function(){
return toSimpleText(this.toString());
};
Number.prototype.toSimpleText = function(){
return toSimpleText(this.toString());
};
Object.prototype.toSimpleText = function(){
if(!!this.childNodes && (!!this.innerText || this.innerText == '')){
if(this.childNodes.length > 0 && this.innerText.length > -1){
return toSimpleText(this.innerText || this.textContent);
};
};
};
if(typeof jQuery != 'undefined'){
jQuery.fn.extend({
toSimpleText: function() {
var $this = $(this);
var result = '';
$this.each(function(index, element){
result += (element.innerText || element.textContent) + ' ';
});
return toSimpleText(result);
}
});
};
});
</script>
</body>
</html>
// to-simple-text.js v1.2.3
function toSimpleText(text){
if( text == undefined || text == null ){
return '';
};
text = text.toString();
text = text.replace(/[\s\uFEFF\xA0]+/g, ' ').replace(/(^ +| +$)/g, '');
return text;
};
String.prototype.toSimpleText = function(){
return toSimpleText(this.toString());
};
Number.prototype.toSimpleText = function(){
return toSimpleText(this.toString());
};
Object.prototype.toSimpleText = function(){
if(!!this.childNodes && (!!this.innerText || this.innerText == '')){
if(this.childNodes.length > 0 && this.innerText.length > -1){
return toSimpleText(this.innerText || this.textContent);
};
};
};
if(typeof jQuery != 'undefined'){
jQuery.fn.extend({
toSimpleText: function() {
var $this = $(this);
var result = '';
$this.each(function(index, element){
result += (element.innerText || element.textContent) + ' ';
});
return toSimpleText(result);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment