Skip to content

Instantly share code, notes, and snippets.

@ShahinSorkh
Last active October 6, 2020 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShahinSorkh/09c5803890b7370d5a5d7c4d24a35520 to your computer and use it in GitHub Desktop.
Save ShahinSorkh/09c5803890b7370d5a5d7c4d24a35520 to your computer and use it in GitHub Desktop.
Simple interactive regex test on browser
<!doctype html>
<html>
<head>
<style>
html { background-color: #2f3131; }
textarea {
display: block;
padding: 15px;
margin: 20px 10px;
width: 100%;
font-size: 14pt;
border: 1px solid #2f7913;
}
span {
margin: 5px;
display: inline-block;
background-color: #2f7913;
padding: 2px
}
</style>
</head>
<body>
<textarea id="in" oninput="match()" rows="2"></textarea>
<textarea id="in-test" oninput="match()" rows="10"></textarea>
<div id="res"></div>
<script>
function match() {
const input = document.querySelector('#in')
const output = document.querySelector('#res')
const testStr = document.querySelector('#in-test').value
output.innerHTML = ''
const regex = new RegExp(input.value, 'gmi')
const result = testStr.match(regex)
for (res of result) {
const box = document.createElement('span')
box.innerHTML = res
box.title = res.split('').map(c => c.charCodeAt(0)).join(' ')
output.appendChild(box)
}
}
match()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment