-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREGEXSynonymReplaceFunction.js
45 lines (31 loc) · 1.08 KB
/
REGEXSynonymReplaceFunction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//synonym replace function needs to run interatively
//for each synonym on bigList[][]
//can be more efficient if we replace item with highest index first,
//then we dont have to keep refreshing page
var bigList = vocabList;
$(document).ready(function(){
var page = $("body").html();
//var i = 0;
var i = Math.floor(Math.random()*10);
for(;i<bigList.length;i+=10){
var syno = bigList[i][0];
var synPos = page.toLowerCase().indexOf(" " + syno + " ");
//skips words that are already replaced
if (page.substr(synPos+syno.length+2,7)=="</span>"){continue;}
if (synPos !== -1 ){
var ED = 1 + Math.floor(Math.random() * (bigList[i].length-1));
var regex = new RegExp(" " + syno + " ", "i");
page =
page.replace(
//page.substr(synPos,syno.length + 2),
regex,
"<span data-tooltip class='has-tip' data-width='300' title=" + syno + "> " + bigList[i][ED] + " </span>"
);
console.log(syno,bigList[i][ED]);
}
}
$("body").html(page);
});
/* How to deal with first letter capitalization:
var syno = syno[0].toUpperCase() + syno.substring(1);
*/