JSFiddle - React, Tailwind, and code Playground
HTML
<p>私は "太郎" です</p>
<script>
// HTML特殊文字変換(通常文字 → 特殊文字)
var htmlsce = (function() {
const map = {' ':' ','<':'<','>':'>','&':'&','"':'"',"'":''','©':'©'};
return function(text, charset) {
charset = charset && charset.replace(/([\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F])/g, '\\$&') || '';
return text.replace(new RegExp('[ <>&"\'©'+charset+']', 'g'), function(match) {
if (map.hasOwnProperty(match)) {
return map[match];
} else {
return '&#'+match.charCodeAt(match)+';';
}
});
};
})();
// HTML特殊文字変換(特殊文字 → 通常文字)
var htmlscd = (function() {
const re = /&#x([0-9A-Fa-f]+);|&#(\d+);|&\w+;/g;
const map = {' ':' ','<':'<','>':'>','&':'&','"':'"',''':"'",'©':'©'};
return function(text) {
return text.replace(re, function(match, p1, p2) {
if (match.charAt(1) == '#') {
// 数値文字参照
if (match.charAt(2) == 'x') {
return String.fromCharCode(parseInt(p1, 16));
} else {
return String.fromCharCode(p2-0);
}
} else if (map.hasOwnProperty(match)) {
// 定義済み文字実体参照
return map[match];
}
return match;
});
};
})();
// p を textarea に置換
const val = $('p').text();
const placeholder = htmlsce(val);
$('p').replaceWith( `<textarea class="inp_name" placeholder="${placeholder}">${val}</textarea>` );
// 「placeholderの初期値」と「textareaの入力値」が同じかどうか
const inpName = $('.inp_name').val();
const iniName = htmlscd( $('.inp_name').attr('placeholder') );
const isSame = inpName === iniName;
console.log( inpName ); // 私は "太郎" です
console.log( iniName ); // 私は "太郎" です
console.log( isSame ); // false
</script>