Skip to content

Instantly share code, notes, and snippets.

@hughshen
Last active October 25, 2016 13:44
Show Gist options
  • Save hughshen/07ea4b650334b3bbe07bfb281e53fa02 to your computer and use it in GitHub Desktop.
Save hughshen/07ea4b650334b3bbe07bfb281e53fa02 to your computer and use it in GitHub Desktop.
Simple regex tester. Textarea highlight inspire by http://codersblock.com/blog/highlight-text-inside-a-textarea
<?php
if (count($_POST) > 0) {
$res = '';
$text = isset($_POST['text']) ? $_POST['text'] : '';
$pattern = isset($_POST['pattern']) ? $_POST['pattern'] : '';
$eachLine = isset($_POST['each_line']) ? (int)$_POST['each_line'] : 0;
if ($pattern) {
if ($eachLine == 1) {
$text = nl2br($text);
$textArr = explode('<br />', $text);
foreach ($textArr as $key => $val) {
$textArr[$key] = @preg_replace('/(' . $pattern. ')/s', '<mark>\1</mark>', $val);
}
$res = implode('', $textArr);
} else {
$res = @preg_replace('/(' . $pattern. ')/s', '<mark>\1</mark>', $text);
}
}
echo $res;
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Regex</title>
<meta charset="utf-8">
<style>
*, *::before, *::after {
box-sizing: border-box;
}
html, body {
height: 100%;
}
body {
font-size: 17px;
background-color: #ddd;
}
.head, .container {
width: 800px;
}
.head {
margin: 4rem auto 2rem auto;
}
.head input[type="text"] {
height: 2.5rem;
width: 100%;
padding: .75rem;
font-size: 1.3rem;
line-height: 2rem;
border: 0;
outline: none;
}
.container {
height: 450px;
max-width: 100%;
margin: 0 auto;
position: relative;
transform: translateZ(0);
-webkit-text-size-adjust: none;
}
.backdrop, textarea {
width: 100%;
height: 100%;
}
.backdrop {
position: absolute;
z-index: 1;
background-color: #fff;
overflow: auto;
pointer-events: none;
transition: transform 1s;
}
.highlights {
white-space: pre-wrap;
word-wrap: break-word;
color: transparent;
}
.highlights, textarea {
padding: .75rem;
font-size: 1.3rem;
line-height: 2rem;
}
textarea {
display: block;
position: absolute;
z-index: 2;
margin: 0;
color: #444;
background-color: transparent;
overflow: auto;
resize: none;
transition: transform 1s;
border: none;
outline: none;
}
mark {
color: transparent;
background-color: #6cf;
}
textarea:focus, input:focus {
box-shadow: 0 0 7px -2px #222;
}
.bold {
font-weight: bold;
}
</style>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<form action="###" onsubmit="return false;" id="regex-form">
<div class="head">
<p class="bold">Expression</p>
<input type="text" name="pattern" class="pattern" value="[A-Z].*?\b">
<p class="bold">Options</p>
<label for="each-line">
<input type="checkbox" name="each_line" id="each-line" class="each_line" value="1"> Each line
</label>
</div>
<div class="container">
<div class="backdrop">
<div class="highlights"></div>
</div>
<textarea name="text" spellcheck="false">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea>
</div>
</form>
<script>
// http://codersblock.com/blog/highlight-text-inside-a-textarea/
;(function($) {
var $form = $('#regex-form');
var $container = $('.container');
var $backdrop = $('.backdrop');
var $highlights = $('.highlights');
var $textarea = $('textarea');
var $pattern = $('.pattern');
var $eachLine = $('.each_line');
var ua = window.navigator.userAgent.toLowerCase();
var isIE = !!ua.match(/msie|trident\/7|edge/);
var isWinPhone = ua.indexOf('windows phone') !== -1;
var isIOS = !isWinPhone && !!ua.match(/ipad|iphone|ipod/);
bindEvents();
handleInput();
function handleInput() {
$.ajax({
url: window.location.pathname,
type: 'post',
data: $form.serialize(),
// dataType: 'json',
success: function(res) {
if (isIE) res = res.replace(/ /g, ' <wbr>');
$highlights.html(res);
},
error: function(msg) {
console.log(msg.reponseText);
}
});
}
function handleScroll() {
var scrollTop = $textarea.scrollTop();
var scrollLeft = $textarea.scrollLeft();
$backdrop.scrollTop(scrollTop);
$backdrop.scrollLeft(scrollLeft);
}
function fixIOS() {
$highlights.css({
'padding-left': '+=3px',
'padding-right': '+=3px'
});
}
function bindEvents() {
$textarea.on({
'input': handleInput,
'scroll': handleScroll
});
$pattern.on({
'input': handleInput
});
$eachLine.on({
'change': handleInput
});
}
if (isIOS) {
fixIOS();
}
})(jQuery);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment