I have here a javascript replace script that is activated via onclick of a button.
I'm using this to easily replace numbers based on a long list of numbers.
What I'm doing is:
I have this list (please know that this is a sample, there's about a thousand more)
.replaceAll("10,", "80,")
.replaceAll("1,", "75,")
.replaceAll("0,", "65,")
.replaceAll("83,", "50,")
If I enter 10 in textarea, and if I click the "Update button", it should become 80 and not 865 or 7565 since I need an exact value. Right now this is what it's doing, changing 10 to 80 and then the 0 in the 80 into 65.
I'm looking for a quick or clean way of doing this using RegEx, to maybe add to the script that I only want to change the specified numbers in the quote marks and not those with preceding or following numbers.
Here's my sample code:
function fixTextarea(textarea) {
textarea.value = textarea.value.replaceAll("3d ed", "4th ed")
.replaceAll("437,", "511,")
.replaceAll("438,", "512, 517,")
.replaceAll("21,", "24,")
.replaceAll("309,", "357,")
.replaceAll("425,", "491,")
.replaceAll("427,", "493,")
.replaceAll("430,", "496-502,")
.replaceAll("10,", "80,")
.replaceAll("1,", "75,")
.replaceAll("0,", "65,")
.replaceAll("83,", "50,")
};
function fixtext() {
let textarea = document.getElementById("textarea1");
textarea.select();
fixTextarea(textarea);
}
window.addEventListener('DOMContentLoaded', function(e) {
var area = document.getElementById("textarea1");
var getCount = function(str, search) {
return str.split(search).length - 1;
};
var replace = function(search, replaceWith) {
if (typeof(search) == "object") {
area.value = area.value.replace(search, replaceWith);
return;
}
if (area.value.indexOf(search) >= 0) {
var start = area.selectionStart;
var end = area.selectionEnd;
var textBefore = area.value.substr(0, end);
var lengthDiff = (replaceWith.length - search.length) * getCount(textBefore, search);
area.value = area.value.replace(search, replaceWith);
area.selectionStart = start + lengthDiff;
area.selectionEnd = end + lengthDiff;
}
};
});
<textarea id="textarea1" rows="4" cols="50">Witkin & Epstein, Criminal Law (3d ed), Pretrial Proceedings §§ 10, 438,</textarea>
<button class="nbtngreen" onclick="fixtext()">Update</button>
I've tried using something like .replaceAll("/^10,\z/", "80,") but it doesn't seem to work.
I'd appreciate any help. I'm stuck with this and I really cannot solve it on my own anymore.