How to replace exact values for .replace script using regex?

Viewed 110

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.

1 Answers

One option could be to create a Map where you would specify what to replace.

Then you could use a pattern to match 1 or more digits, preceded by a word boundary \b to prevent a partial match and assert a comma directly to the right (?=,) using a positive lookahead.

In the callback of replace, check if the string exists in the Map. Return the mapped value if it does, else return the original match to leave it unchanged.

Press the button once to do all the replacements.

\b\d+(?=,)

Regex demo

For example:

const mapper = new Map();
mapper.set("10", "80");
mapper.set("437", "511");
mapper.set("80", "81");

function fixtext() {
  const elm = document.getElementById("textarea1");
  if (elm) {
    elm.value = elm.value
      .replace(
        /\b\d+(?=,)/g,
        m => mapper.has(m) ? mapper.get(m) : m
      );
  }
}
<textarea id="textarea1" rows="4" cols="50">Witkin & Epstein, Criminal Law (3d ed), Pretrial Proceedings §§ 10,438, this should not change 437 and this should: 437,9999</textarea>

<button class="nbtngreen" onclick="fixtext()">Update</button>

Related