JavaScript Strings - Exercise 3

You can check if a string contains only alphabetic characters in JavaScript using regular expressions.

javascript

var str = 'HelloWorld';
var isAlphabetic = /^[a-zA-Z]+$/.test(str); // isAlphabetic will be true

You can check if a string contains only numeric characters in JavaScript using regular expressions.

javascript

var str = '12345';
var isNumeric = /^[0-9]+$/.test(str); // isNumeric will be true

You can convert a string to title case by lowercasing the string first and then capitalizing the first letter of each word.

javascript

function toTitleCase(str) {
              return str
                .toLowerCase()
                .split(' ')
                .map(function (word) {
                  return word ? word[0].toUpperCase() + word.slice(1) : word;
                })
                .join(' ');
            }

            const titleCaseStr = toTitleCase('hello world'); // 'Hello World'

You can remove non-alphanumeric characters from a string in JavaScript using regular expressions.

javascript

var str = 'hello123@world!';
var alphanumericStr = str.replace(/[^a-zA-Z0-9]/g, ''); // alphanumericStr will be 'hello123world'

You can remove a specific character with replaceAll() if you want every occurrence, or replace() if you only want the first one.

javascript

const str = 'hello world';
const removedCharStr = str.replaceAll('o', ''); // 'hell wrld'

You can find the longest word in a string in JavaScript by splitting the string into words and then iterating over them to find the longest word.

javascript

function findLongestWord(str) {
    var words = str.split(' ');
    var longestWord = '';
    for (var i = 0; i < words.length; i++) {
        if (words[i].length > longestWord.length) {
            longestWord = words[i];
        }
    }
    return longestWord;
}

var longest = findLongestWord('The quick brown fox jumps over the lazy dog'); // longest will be 'quick'

A common approach is to normalize both strings, alphabetize their characters, and compare the results.

javascript

function areAnagrams(str1, str2) {
              const normalize = function (value) {
                return value.toLowerCase().replace(/\s/g, '').split('').sort().join('');
              };

              return normalize(str1) === normalize(str2);
            }

            const result = areAnagrams('listen', 'silent'); // true

You can check if a string is a palindrome in JavaScript by comparing it to its reverse.

javascript

function isPalindrome(str) {
    return str === str.split('').reverse().join('');
}

var result = isPalindrome('radar'); // result will be true

You can count the occurrences of a substring within a string in JavaScript using regular expressions or by iterating over the string.

javascript

function countOccurrences(str, subStr) {
    return str.split(subStr).length - 1;
}

var count = countOccurrences('hello world hello', 'hello'); // count will be 2

You can remove duplicate characters from a string in JavaScript by iterating over the string and maintaining a set of unique characters.

javascript

function removeDuplicates(str) {
    var uniqueChars = '';
    for (var i = 0; i < str.length; i++) {
        if (uniqueChars.indexOf(str[i]) === -1) {
            uniqueChars += str[i];
        }
    }
    return uniqueChars;
}

var result = removeDuplicates('hello world'); // result will be 'helo wrd'