Skip to content

Instantly share code, notes, and snippets.

@kosalvann
Created May 15, 2020 13:57
Show Gist options
  • Save kosalvann/b00541e4c6beefca3e036a93d8a0ae1e to your computer and use it in GitHub Desktop.
Save kosalvann/b00541e4c6beefca3e036a93d8a0ae1e to your computer and use it in GitHub Desktop.
Parse Message Variable Using regex to identify a specific message variable // source https://jsbin.com/caceged
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Using regex to identify a specific message variable">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Parse Message Variable</title>
<style id="jsbin-css">
html, body {
padding: 20px;
margin: 0;
height: 100%;
}
body * {
font-size: 15px;
font-family: arial, sans;
line-height: 1.6;
box-sizing: border-box;
}
</style>
</head>
<body>
<div id="app"></div>
<script id="jsbin-javascript">
/**
* A message variable is an alphanumeric string that
* must start and end with a percent sign, ie %example%.
*/
const paragraph = `This is a message string containing some message variables, like %this% and %firstName% in camel case. Other variables can have dashes, like %abc-123-xyz%. We'll discard ones that are clearly a percent number, like 20%.`;
/**
* The regular expression pattern use
* to match any message variable
*/
const regex = /%[a-zA-Z0-9-_]+%/g;
/**
* Display the list of message variables on page load
*/
window.addEventListener('load', () => {
document.querySelector('#app')
.innerHTML = parseMessageVariables(paragraph)
})
/**
* Parse a message string for message variables
*
* @param string message The message string
* @return array A list of message variables
*/
parseMessageVariables = ((message) => {
console.log(message.match(regex));
// return message.match(regex)
})
</script>
<script id="jsbin-source-css" type="text/css">html, body {
padding: 20px;
margin: 0;
height: 100%;
}
body * {
font-size: 15px;
font-family: arial, sans;
line-height: 1.6;
box-sizing: border-box;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">/**
* A message variable is an alphanumeric string that
* must start and end with a percent sign, ie %example%.
*/
const paragraph = `This is a message string containing some message variables, like %this% and %firstName% in camel case. Other variables can have dashes, like %abc-123-xyz%. We'll discard ones that are clearly a percent number, like 20%.`;
/**
* The regular expression pattern use
* to match any message variable
*/
const regex = /%[a-zA-Z0-9-_]+%/g;
/**
* Display the list of message variables on page load
*/
window.addEventListener('load', () => {
document.querySelector('#app')
.innerHTML = parseMessageVariables(paragraph)
})
/**
* Parse a message string for message variables
*
* @param string message The message string
* @return array A list of message variables
*/
parseMessageVariables = ((message) => {
console.log(message.match(regex));
// return message.match(regex)
})
</script></body>
</html>
html, body {
padding: 20px;
margin: 0;
height: 100%;
}
body * {
font-size: 15px;
font-family: arial, sans;
line-height: 1.6;
box-sizing: border-box;
}
/**
* A message variable is an alphanumeric string that
* must start and end with a percent sign, ie %example%.
*/
const paragraph = `This is a message string containing some message variables, like %this% and %firstName% in camel case. Other variables can have dashes, like %abc-123-xyz%. We'll discard ones that are clearly a percent number, like 20%.`;
/**
* The regular expression pattern use
* to match any message variable
*/
const regex = /%[a-zA-Z0-9-_]+%/g;
/**
* Display the list of message variables on page load
*/
window.addEventListener('load', () => {
document.querySelector('#app')
.innerHTML = parseMessageVariables(paragraph)
})
/**
* Parse a message string for message variables
*
* @param string message The message string
* @return array A list of message variables
*/
parseMessageVariables = ((message) => {
console.log(message.match(regex));
// return message.match(regex)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment