0

I am trying to create a regular expression for finding anything that is not a letter, and I also want to detect any white spaces in my string. I have looked at the MDN documentation, youtube videos, and stackoverflow questions, and I still don't understand. Could you point me in the right direction? Here is the jsfiddle(it is a function for palindromes.): https://jsfiddle.net/jn2tutty/71/

Here is the code:

var regexp = /[^A-Za-z]|\s+|/g;

1
  • White space characters are not letters, so there's no need to test for them separately. Commented Jul 31, 2016 at 1:46

2 Answers 2

1

This approach discards everything but letters and uses a for loop to reverse the string.

var str = ' some123 text -&';
str = str.replace(/[^a-z]/gi,'').toLowerCase();
console.log(str);
var i,reversed = '';
for (i = str.length-1; i > -1; i--) {
        reversed += str[i];
}
if (str === reversed) {
        console.log('palindrome');
} else {
        console.log('not a palindrome');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your regex is correct. Your code/fiddle does not work because the strings have different casing (Racecar vs racecar).

If you also convert the nospace variable to lowercase, it works:

function palindrome(str) {
  // Good luck!
  var nospace = str.replace(" ", "").toLowerCase();
                                 // ^^^^^^^^^^^^^^--- added this
  var reverse = str.split('').reverse().join('');
  var change = reverse.toLowerCase();
  console.log(change);
  var regexp = /[^A-Za-z]|\s+/g;

  var x = change.replace(regexp, "");

  if (nospace == x) {
    return true;
  } else {
    return false;
  }
}
console.log(palindrome("Race car"));

See updated fiddle here.


Now, your code could use some other improvements.

The regex "strip everything that is not a letter" regex (/[^A-Za-z]|\s+/g) could be simplified to /[^A-Za-z]/g. But you may really not need it at all (I don't know your requirements, you may still need it if your requirements really want you to remove non-letters, not just spaces).

Your first nospace var only replaces the first white space from the input. Using a regex there would make it replace all of them (only do this if this is really what you wanted): str.replace(" ", "") becomes str.replace(/\s+/g, "").

Also, since you already removed the spaces in nospace, you could just reverse it (instead of reversing the input and later removing its spaces). And then compare it.

Also the last if/else could be simplified to just a return statement. The final code would look something like this:

function palindrome(str) {
  var nospace = str.replace(/\s+/g, "").toLowerCase();
  var reverse = nospace.split('').reverse().join('').toLowerCase().replace(/[^a-z]/g, "");
  //          optional, depends on your requirements -------------^^^^^^^^^^^^^^^^^^^^^^^^
  return nospace === reverse;
}
console.log(palindrome("R a c e    c a r")); // output: true

JSFiddle here.

2 Comments

Yeah, good point. I was thinking on updating the answer with that remark. I think I will now, thanks!
Thanks alot. This helped tremendously. I am still learning :)

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.