Created
May 20, 2017 21:06
-
-
Save yifanz/32e46e265d467ee96e96b4aab00b3356 to your computer and use it in GitHub Desktop.
Palindrome Highlighter
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="author" content="Yi-Fan Zhang" /> | |
<title>Palindromes</title> | |
<style> | |
.editor { | |
position: relative; | |
} | |
.editor textarea { | |
resize: none; | |
font-family: inherit; | |
font-size: inherit; | |
} | |
.editor .editor-common { | |
position: absolute; | |
top: 0; | |
left: 0; | |
background-color: transparent; | |
margin: 0; | |
padding: 0; | |
height: 150px; | |
width: 500px; | |
overflow: hidden; | |
white-space: pre-wrap; | |
word-wrap: break-word; | |
} | |
.editor .editor-front { | |
background-color: transparent; | |
} | |
.editor .editor-back { | |
border: 1px solid grey; | |
color: transparent; | |
} | |
.highlight { | |
background-color: yellow; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="editor"> | |
<div id="editor-textarea-back" class="editor-back editor-common"></div> | |
<textarea id="editor-textarea" class="editor-front editor-common">Here are some palindromes: racecar, deed and eye. If you can think of some more, type them in here.</textarea> | |
</div> | |
</body> | |
<script type="text/javascript"> | |
(function() { | |
var textarea = document.getElementById("editor-textarea"); | |
var textarea_back = document.getElementById("editor-textarea-back"); | |
var timer; | |
update(); | |
textarea.addEventListener("keydown", update); | |
function update() { | |
textarea_back.innerHTML = ""; | |
if (timer) clearTimeout(timer); | |
timer = setTimeout(function() { | |
textarea_back.innerHTML = highlight_palindrome(textarea.value); | |
textarea_back.scrollTop = textarea.scrollTop; | |
}, 500); | |
} | |
function highlight_palindrome(str) { | |
var lines = str.split(/\n/); | |
var i, j, words, word, match, new_words, new_lines = []; | |
for (i = 0; i < lines.length; i++) { | |
words = lines[i].split(/\s/); | |
new_words = []; | |
for (j = 0; j < words.length; j++) { | |
word = words[j]; | |
match = word.match(/([A-Za-z]+)/); | |
if (match && match[0].length > 2 | |
&& is_palindrome(match[0].toLowerCase())) { | |
word = word.slice(0, match.index) | |
+ '<span class="highlight">' | |
+ word.substring(match.index, match[0].length) | |
+ '</span>' | |
+ word.substring(match.index + match[0].length); | |
} | |
new_words.push(word); | |
} | |
new_lines.push(new_words.join(' ').replace(/\n/g,'<br/>')); | |
} | |
return new_lines.join('<br/>') + '<br/>'; | |
} | |
function is_palindrome(word) { | |
if (word.length < 2) { | |
return true; | |
} else if (word[0] === word[word.length - 1]) { | |
return is_palindrome(word.slice(1,-1)); | |
} else { | |
return false; | |
} | |
} | |
})(); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment