Blog

Determine if a String Contains a Palindrome in JavaScript
Posted on December 7, 2017 in Algorithms, JavaScript, Regular Expressions by Matt Jennings

  • Determine if a string contains a palindrome in JavaScript.
  • Strip out any non Latin alphabetic characters if needed.
var str = '!Noel - sees Leon';

function getPalinedrome(str) { 
    var str1 = str.replace(/[^a-z]/gi, '').toLowerCase();
    
    var str1Reverse = str1.split('').reverse().join('');
      
    if(str1 === str1Reverse) {
      return true;
    }
    else {
      return false;
    }   
}

// Output true
console.log(getPalinedrome(str));

Leave a Reply