0

I am creating a work tool for notepad abbreviations. As the company I work for is strict about downloading any external tools I've resorted to using Javascript and HTML built on notepad.

I've been able to replace single words such as when I type "Vacancy" it returns "VAC". Or when typing "Payment" it returns "PYMT". My issue is trying to replace multiple words into 1 small abbreviation. For instance "Follow Up" I want to return "F/U". With the spaces I'm finding it is not working.

Tried multiple ways but unable to figure this out.

Here is the code snippet that I've used

function myFunction() {

var str = document.getElementById("demo").value; 
var mapObj = {
   Payment:"PYMT",
   Vacancy:"VAC", 
str = str.replace(/Payment|Vacancy, fucntion(matched){
  return mapObj[matched];
});
alert(str);
  document.getElementById("demo").value = res;
}

What I would like to do is add my mabObj so it would read

function myFunction() {

var str = document.getElementById("demo").value; 
var mapObj = {
Follow Up:"F/U"
str = str.replace(/Follow Up|, fucntion(matched){
  return mapObj[matched];
});
alert(str);
  document.getElementById("demo").value = res;
}
2
  • 1
    Your code have a lot of syntax errors. However, try using quotes in your object definition, like var mapObj = {"Follow Up":"F/U" }. Then you can do: str = str.replace(/Follow Up/, function(matched) {return mapObj[matched];}); Commented Sep 27, 2019 at 21:47
  • 2
    Lots of typos. Missing / at the end of the first regexp, you misspelled function as fucntion. Commented Sep 27, 2019 at 21:48

2 Answers 2

1

JavaScript objects can have properties with spaces in them, but in order to do so, the property name needs to have quotes around it.

That said, I would suggest using a Map in this case, as it will allow you to match any string without worrying about naming collisions with properties from the object's prototype.

const abbreviation = new Map([
    ['Follow Up', 'F/U'],
    ['Payment', 'PYMT'],
    ['Vacancy', 'VAC']
]);
const input = 'Payment noise Vacancy noise Follow Up noise Vacancy';
const pattern = new RegExp(Array.from(abbreviation.keys()).join('|'),'g');
const result = input.replace(pattern, (matched) => {
    return abbreviation.get(matched) || matched;
});
console.log(result);  // 'PYMT noise VAC noise F/U noise VAC'
Sign up to request clarification or add additional context in comments.

2 Comments

new RegExp(Array.from(abbreviation.keys()).join('|'),'g') if you're generating a regex dynamically from the names, then you have to make sure they are escaped, otherwise "Mr." might match "Mrs", for example.
True, @VLAZ. Escaping regex is non-trivial, so instead I've added || matched to my answer, in the replacer function. That ensures that only exact matches have any effect. It will cause Mrs to be returned instead of undefined when Mr. matches it and thus that part of the string will remain unchanged.
0

To include a key with a space in an object you can put it in brackets like {["Follow Up"]: "F/U"}

function replaceKeyWords(str) {
  var mapObj = {
     Payment:"PYMT",
     Vacancy:"VAC",
     ["Follow Up"]:"F/U",
  };
  str = str.replace(/(Payment|Vacancy|Follow Up)/, function(matched){
    return mapObj[matched];
  });
  return str;
}

console.log(replaceKeyWords("Payment"));
console.log(replaceKeyWords("Vacancy"));
console.log(replaceKeyWords("Follow Up"));

2 Comments

You don't need brackets, only quotes. The brackets are for computed property names but you are using a static string, so there is no need for evaluation.
Oh interesting. Thanks for the note.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.