Skip to content

Instantly share code, notes, and snippets.

@met
Last active January 11, 2021 18:26
Show Gist options
  • Save met/0943f448e95db7e19a70742abc098039 to your computer and use it in GitHub Desktop.
Save met/0943f448e95db7e19a70742abc098039 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/defaliv replace some words inside textnode
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<article>
<p>The production effect has been replicated in numerous studies spanning more than
a decade. <a href="https://uwaterloo.ca/">In one study in Australia</a>, a group of
seven-to-10-year-olds were presented with a list of words and asked to read some silently,
and others aloud. Afterwards, they correctly recognised 87% of the words they’d read aloud,
but only 70% of the silent ones.</p>
<p>MacLeod has named this phenomenon the <a href="https://uwaterloo.ca/">“production effect”.</a> It means
that producing written words – that’s to say, reading them out loud – improves our memory of them.</p>
<p>In another study, <a href="https://uwaterloo.ca/">adults aged 67 to 88</a> were given
the same task – reading words either silently or aloud – before then writing down all
those they could remember. They were able to recall 27% of the words they had read aloud,
but only 10% of those they’d read silently. When asked which ones they recognised,
they were able to correctly identify 80% of the words they had read aloud,
but only 60% of the silent ones. MacLeod and his team have found the effect can last up to
a week after the reading task.</p>
<p><!-- Komentar text node. Komentar text node. --></p>
<p><style>Style text node. Style text node.</style></p>
<p><script>/* Script text node. Script text node. */</script></p>
<p><noscript>/* Script text node. Script text node. */</noscript></p>
</article>
<script id="jsbin-javascript">
"use strict";
let myNode = document.querySelector("article p").firstChild;
processTextNode(myNode);
function processTextNode(node) {
//console.log(node.wholeText);
let text = node.wholeText;
let rv = text.replace(
/(\S+)/g,
function(m,p,o,s) {
// m = word match, p = match inside brackets, same as m here, o = index of match starting position, s = whole string
//console.log({m, o,s})
const minimumLength = 2;
// who many words to change
// 0 = nothing
// 0.25 = 25%
// 0.5 half
// 1 every word
const howMany = 0.3;
// "dash" must be in the first array field, to correctly form the reg. exp.
let specialCharacters = ["-", "(", ")", ",", "\"", "'", ":", ";", ".", "?", "!", "¡", "¿", "0-9"];
let re = new RegExp("[" + specialCharacters.join("") + "]");
if (m.length < minimumLength) {
return m; // unchanged
}
if (m.match(re)) {
return m;
}
if (Math.random() > howMany) {
return m;
}
else {
// change the word
// TODO do not replace with undescores, but with span elements
let replaced = m.replace(/./g, "_")
return replaced;
}
}
);
console.log("Node match: " + rv);
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">"use strict";
let myNode = document.querySelector("article p").firstChild;
processTextNode(myNode);
function processTextNode(node) {
//console.log(node.wholeText);
let text = node.wholeText;
let rv = text.replace(
/(\S+)/g,
function(m,p,o,s) {
// m = word match, p = match inside brackets, same as m here, o = index of match starting position, s = whole string
//console.log({m, o,s})
const minimumLength = 2;
// who many words to change
// 0 = nothing
// 0.25 = 25%
// 0.5 half
// 1 every word
const howMany = 0.3;
// "dash" must be in the first array field, to correctly form the reg. exp.
let specialCharacters = ["-", "(", ")", ",", "\"", "'", ":", ";", ".", "?", "!", "¡", "¿", "0-9"];
let re = new RegExp("[" + specialCharacters.join("") + "]");
if (m.length < minimumLength) {
return m; // unchanged
}
if (m.match(re)) {
return m;
}
if (Math.random() > howMany) {
return m;
}
else {
// change the word
// TODO do not replace with undescores, but with span elements
let replaced = m.replace(/./g, "_")
return replaced;
}
}
);
console.log("Node match: " + rv);
}</script></body>
</html>
"use strict";
let myNode = document.querySelector("article p").firstChild;
processTextNode(myNode);
function processTextNode(node) {
//console.log(node.wholeText);
let text = node.wholeText;
let rv = text.replace(
/(\S+)/g,
function(m,p,o,s) {
// m = word match, p = match inside brackets, same as m here, o = index of match starting position, s = whole string
//console.log({m, o,s})
const minimumLength = 2;
// who many words to change
// 0 = nothing
// 0.25 = 25%
// 0.5 half
// 1 every word
const howMany = 0.3;
// "dash" must be in the first array field, to correctly form the reg. exp.
let specialCharacters = ["-", "(", ")", ",", "\"", "'", ":", ";", ".", "?", "!", "¡", "¿", "0-9"];
let re = new RegExp("[" + specialCharacters.join("") + "]");
if (m.length < minimumLength) {
return m; // unchanged
}
if (m.match(re)) {
return m;
}
if (Math.random() > howMany) {
return m;
}
else {
// change the word
// TODO do not replace with undescores, but with span elements
let replaced = m.replace(/./g, "_")
return replaced;
}
}
);
console.log("Node match: " + rv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment