

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript - Find if string is a palindrome (Check for punctuation)
We are required to write a JavaScript function that returns true if a given string is a palindrome. Otherwise, returns false.
These are the conditions we have to keep in mind while validating the string −
We have to remove punctuation and turn everything lower case in order to check for palindromes.
We have to make it case insensitive, such as "racecar", "RaceCar", and "race CAR" among others.
Example
Following is the code −
const str = 'dr. awkward'; const isPalindrome = (str = '') => { const regex = /[^A-Za-z0-9]/g; str = str.toLowerCase().replace(regex, ''); let len = str.length; for (let i = 0; i < len/2; i++) { if (str[i] !== str[len - 1 - i]) { return false; }; }; return true; }; console.log(isPalindrome(str));
Output
Following is the output on console −
true
- Related Questions & Answers
- Check if string begins with punctuation in JavaScript
- Check if a string is palindrome in C using pointers
- C Program to Check if a Given String is a Palindrome?
- Check if a character is a punctuation mark in Arduino
- How to check if String is Palindrome using C#?
- C# program to check if a string is palindrome or not
- Python program to check if a string is palindrome or not
- Python program to check if a given string is number Palindrome
- Recursive function to check if a string is palindrome in C++
- How to find if a string is a palindrome using Java?
- Check if a given string is a rotation of a palindrome in C++
- Check if a number is Palindrome in C++
- TCP Client-Server Program to Check if a Given String is a Palindrome
- Check if a string is sorted in JavaScript
- Python program to check if the given string is vowel Palindrome
Advertisements