<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ayushman Chaturvedi</title>
    <description>The latest articles on Forem by Ayushman Chaturvedi (@ayushman).</description>
    <link>https://forem.com/ayushman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3071914%2F6cdb1e22-a85c-4167-b128-fa2f298ef4b0.png</url>
      <title>Forem: Ayushman Chaturvedi</title>
      <link>https://forem.com/ayushman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ayushman"/>
    <language>en</language>
    <item>
      <title>Measure Distance Between Two Locations in JavaScript Using the Haversine Formula</title>
      <dc:creator>Ayushman Chaturvedi</dc:creator>
      <pubDate>Sat, 26 Apr 2025 12:02:14 +0000</pubDate>
      <link>https://forem.com/ayushman/measure-distance-between-two-locations-in-javascript-using-the-haversine-formula-7dc</link>
      <guid>https://forem.com/ayushman/measure-distance-between-two-locations-in-javascript-using-the-haversine-formula-7dc</guid>
      <description>&lt;p&gt;Have you ever been curious to explore about how far two places are from each other just by using their latitude and longitude? Maybe you're working on a map app, building a travel planner, or simply wondering how far apart your favorite cities are.&lt;/p&gt;

&lt;p&gt;The most reliable way to calculate the distance between two points on the globe using Javascript is by using the Haversine equation. As long as you have the latitude and longitude for your starting point and destination, you can easily compute the distance between them.&lt;/p&gt;

&lt;p&gt;Here’s how to calculate the distance in kilometers between two locations using JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;p&gt;When you get location data (gotten using the Geolocation API), the coordinates come in degrees. But the Haversine formula expects radians. So first, we'll write a little helper function to convert degrees to radians.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function degToRad(deg) {
  var rad = (deg * Math.PI)/180;
  return rad;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 2
&lt;/h1&gt;

&lt;p&gt;Now, let’s create the main function that actually calculates the distance between two points.&lt;/p&gt;

&lt;p&gt;We’ll use the Haversine formula, which takes into account the spherical shape of the Earth (because the Earth isn’t flat... sorry if that's news 😄).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Calculate distance between two coordinates
function calculateDistance(startCoords, destCoords) {
  const startingLat = degToRad(startCoords.latitude);
  const startingLong = degToRad(startCoords.longitude);
  const destinationLat = degToRad(destCoords.latitude);
  const destinationLong = degToRad(destCoords.longitude);

  // Radius of the Earth (in kilometers)
  const radius = 6371;

  // Haversine formula
  const distance = Math.acos(
    Math.sin(startingLat) * Math.sin(destinationLat) +
    Math.cos(startingLat) * Math.cos(destinationLat) *
    Math.cos(startingLong - destinationLong)
  ) * radius;

  return distance;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 3
&lt;/h1&gt;

&lt;p&gt;Now that we have our calculateDistance function, let's try it out with some sample coordinates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const start = {
  latitude: 58.39343,
  longitude: -259.2627
};

const destination = {
  latitude: 43.8394,
  longitude: -129.3984
};

const distance = calculateDistance(start, destination);

console.log(`The distance between both locations is ${distance.toFixed(2)} km.`);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>JavaScript Regular Expressions Made Simple</title>
      <dc:creator>Ayushman Chaturvedi</dc:creator>
      <pubDate>Tue, 22 Apr 2025 10:35:16 +0000</pubDate>
      <link>https://forem.com/ayushman/javascript-regular-expressions-made-simple-f3n</link>
      <guid>https://forem.com/ayushman/javascript-regular-expressions-made-simple-f3n</guid>
      <description>&lt;p&gt;If you’ve ever worked with strings in JavaScript—maybe trying to check if an email is valid in a form or clean up some messy input—you’ve probably run into something called regular expressions, or regex.&lt;/p&gt;

&lt;p&gt;At first glance, regex looked like a bunch of gibberish to me—like what the heck is this—/\d{3}-?\d{3}-?\d{4}/. But later I realised, its not as scary as it seems. In fact, its just a way to describe patterns in text. Let me break it down for you in plain English.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a regular expression&lt;br&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;In JavaScript, a regular expression is an object, constructed with either the RegExp constructor or with forward slash (/) characters enclosing a pattern as a value (literal notation).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let pattern1 = new RegExp("hello");
let pattern2 = /hello/;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both expressions do the same thing—they look for the word “hello” in a string. The second one is shorter and more commonly used.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Matching&lt;br&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Just like normal objects, regular expressions also have methods. The most common method is test(), which accepts a string and returns a Boolean that tells us whether the string matches the pattern in the expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(/cat/.test("black cat")); // true
console.log(/dog/.test("black cat")); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Match Sets of Characters&lt;br&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  SETS OF CHARACTER&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;Placing a set of characters between square brackets matches that part of the regular expression to any of the characters within the brackets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(/[abcdefghijklmnopqrstuvwxyz]/.test("year 2021")); // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  RANGES OF CHARACTER&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;The above expression matches all strings that contain lowercase English letters. We can make the expression shorter by using a hyphen (-). A hyphen between two characters between square brackets represents a range of characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/[a-z]/.test("hello123") // true
/[0-9]/.test("hello123") // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a range of characters indicated with a hyphen, the ordering of the characters is determined by their Unicode number. For example, characters a-z (codes 97-122) are next to each in the Unicode ordering, and so using range [a-z] includes every character in this range and matches all lowercase Latin letters.&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  CHARACTER GROUPS SHORTHAND&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;In regular expressions, character sets/groups have a built-in shorthand for writing them. Digits ([0-9]) can be represented as \d. Here are some common character sets and what they mean:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Shorthand&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;\d&lt;/td&gt;
&lt;td&gt;Any digit (0-9)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;\w&lt;/td&gt;
&lt;td&gt;Any word character (a-z, A-Z, 0-9, _)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;\s&lt;/td&gt;
&lt;td&gt;Any whitespace character&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;\D&lt;/td&gt;
&lt;td&gt;Anything &lt;em&gt;not&lt;/em&gt; a digit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;\W&lt;/td&gt;
&lt;td&gt;Anything &lt;em&gt;not&lt;/em&gt; a word character&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;\S&lt;/td&gt;
&lt;td&gt;Anything &lt;em&gt;not&lt;/em&gt; a space&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If we want to match a phone number with format XXX-XXX-XXXX, here’s how we can do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let phoneNum = /\d\d\d-\d\d\d-\d\d\d\d/
console.log(phoneNum.test("202-588-6500")); // true
console.log(phoneNum.test("67-500-647")); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  EXCLUDE CHARACTERS&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;The caret (^) character lets us invert a set of characters. That is, it matches any character except the character(s) in the given set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(/[^\d]/.test("ujdhf345kd")); // true
console.log(/[^\d]/.test("3453")); //  false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  SPECIAL CHARACTERS&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;Characters like plus signs (+) and question marks (?) have special meanings in regular expressions and need to be preceded by a backslash if we want to indicate the character itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let helloQuestion = /hello\?/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These shorthand codes can also be used within square brackets to indicate a set of characters. For example, [\d] represents any digit. When special characters like the plus (+) and the question mark (?) are used between square brackets, they lose their special meaning. So, [+?] matches any plus or question mark.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  REPEATED PATTERNS&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;When we want to match things that repeat (like digits in a phone number), we use special symbols:&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  + means "one or more times."&lt;br&gt;&lt;br&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(/\d+/.test("123")); // true (because 1, 2, 3 are digits)
console.log(/\d+/.test("abc")); // false (no digits)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It matches if at least one digit is there.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  * means "zero or more times."&lt;br&gt;&lt;br&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(/a*/.test("aaa"));   // true (matches all a's)
console.log(/a*/.test(""));      // true (zero a's is also allowed!)
console.log(/a*/.test("bbb"));   // true (even though there's no 'a', it matches zero a's)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, it doesn't require the pattern to be present. It's okay if it's there many times, or not at all.&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  We can say how many times something should appear using curly braces {}.
&lt;/h4&gt;

&lt;p&gt;• {3} means exactly 3 times&lt;br&gt;
• {2, 4} means between 2 and 4 times&lt;br&gt;
• {2,} means 2 or more times&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(/\d{3}/.test("123"));    // true (exactly 3 digits)
console.log(/\d{3}/.test("12"));     // false (only 2 digits)
console.log(/\d{2,4}/.test("1234")); // true (4 digits is allowed)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  OPTIONAL CHARACTERS&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;To make a part of a pattern optional, we use the question mark (?). It allows a character to occur zero or one number of times. &lt;br&gt;
For Example, Phone numbers are usually valid even when they are not hyphenated. We can make the hyphen optional. To make a part of a pattern optional, we use the question mark (?).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let phoneNum = /\d{3}-?\d{3}-?\d{4}/
console.log(phoneNum.test("202-588-6500")); //  true
console.log(phoneNum.test("2025886500")); //  true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the pattern matches even when the hyphen character (-) is omitted.&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  GROUP CHARACTERS&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;We use Parentheses to group parts of a pattern, so that symbols like +, *, or {} apply to the entire group, not just a single character. When a part of a regular expression is surrounded by parentheses, it is treated as a single element by any operations following it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let laugh = /(ha)+/;
console.log(laugh.test("hahaha")); // true (group "ha" repeated)
console.log(laugh.test("haa"));    // false ("ha" not repeated properly)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  CASE SENSITIVITY&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;We can add the letter i after the regex to make the pattern case-insensitive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let greet = /hello/i;
console.log(greet.test("HELLO"));  // true
console.log(greet.test("Hello"));  // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  MATCHING WITHIN BOUNDARIES&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;To make a matching span through an entire string, we use:&lt;br&gt;
• ^ → beginning of string&lt;br&gt;
• $ → end of string&lt;br&gt;&lt;br&gt;&lt;br&gt;
We can use both to make sure the whole string matches the pattern, not just part of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let onlyNumbers = /^\d+$/;
console.log(onlyNumbers.test("12345")); // true (only digits)
console.log(onlyNumbers.test("12a45")); // false (has a letter)
console.log(onlyNumbers.test(" 12345")); // false (starts with space)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  WORD BOUNDARIES&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;The marker \b refers to a word boundary, which can be the start or end of the string. \b is like an invisible wall between words. It checks if something is at the start or end of a word. It can also refer to any place in the string that has a word character on one side and a non-word character on the other side.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(/\bcat\b/.test("black cat"));  // true (exact word "cat")
console.log(/\bcat\b/.test("category"));   // false (not a whole word)
console.log(/\bcat/.test("catfish"));      // true (starts with "cat")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ALTERNATIVES WITH THE OR OPERATOR&lt;br&gt;&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;We use the pipe character (|) to indicate a choice between a pattern to its left and that to its right. For example, we can match a text that contains the word “watch” in either its plural (ending with “es”) form, past tense (ending with “ed”), or personal noun (ending with “er”) form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let word = /\b\watch(es|ed|er)?\b/;
console.log(word.test("watch")); // true
console.log(word.test("watched")); // true
console.log(word.test("watching")); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Other methods for matching&lt;br&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  exec()
&lt;/h4&gt;

&lt;p&gt;We already know that the test() method just tells us whether something matches a pattern or not. It always gives result in either true or false. But if we want to see what is actually matched and where it is found in the string, then we use exec() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let execMatch = /\d+/.exec("abc 123");
console.log(execMatch); // Array [ "123" ]
console.log(execMatch.index); // 4
let execMatch2 = /\d+/.exec("abc");
console.log(execMatch2); // null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what’s happening:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;exec() finds "123" in the string "abc 123".&lt;/li&gt;
&lt;li&gt;It gives us an array where the first item is the matched text.&lt;/li&gt;
&lt;li&gt;It also adds a property called index that shows where in the string the match started (position 4 in this case).&lt;/li&gt;
&lt;li&gt;If there’s no match, exec() returns null.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  match()
&lt;/h4&gt;

&lt;p&gt;The match() method works on strings instead of patterns. But it behaves similarly to exec().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("abc 123".match(/\d+/)); // [ "123" ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Match and Replace&lt;br&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Sometimes, we want to replace part of a string with something else — for example, changing "a" to "e" in "haha". JavaScript gives us a method called .replace() for this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("haha".replace("a", "e")); // heha
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, only the first "a" is replaced with "e".&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Using Regex with replace():
&lt;/h4&gt;

&lt;p&gt;We can also use regular expressions as the first argument of replace(). This is powerful because it lets us replace patterns, not just exact text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("hahehahehe".replace(/a/, "e"));
// hehehahehe   (only replaces the first "a")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Replace All Matches with /g
&lt;/h4&gt;

&lt;p&gt;Want to replace every match, not just the first? Add the g (global) flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("hahehahehe".replace(/a/g, "e"));
// hehehehehe   (all "a"s are now "e")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we just want to replace all exact matches (not patterns), we can also use .replaceAll():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("hahehahehe".replaceAll("a", "e"));
// hehehehehe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Using a Function in replace()
&lt;/h4&gt;

&lt;p&gt;Instead of a string, we can also pass a function as the second argument. This lets us do something dynamic with each match.&lt;/p&gt;

&lt;p&gt;Example: Convert some specific words to uppercase:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let phrase = "unicef is a humanitarian ngo.";
let result = phrase.replace(/\b(unicef|ngo)\b/g, word =&amp;gt; word.toUpperCase());
console.log(result);
// UNICEF is a humanitarian NGO.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The regex looks for whole words unicef or ngo.&lt;/p&gt;

&lt;p&gt;The function takes each matched word and returns it in uppercase.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
