Ali Khakpouri
Ali Khakpouri

Reputation: 873

Javascript replacing single quote with empty string

This is an odd one! I kind of want to say the reason my replace function isn't working correctly is because of the font. I've never seen this issue before, and I wonder if I'm overlooking something!?

I have the following variable set to a static text with '.

var lastName = "O'Donnell";

In my browser, console.log(lastName) outputs: O’Donnell. Instead of O'Donnell. Therefore, the following replace method isn't working.

Screenshot: enter image description here

return lastName.replace(/'/g, '')

What am I doing wrong?

Upvotes: 1

Views: 3136

Answers (1)

Sam Jones
Sam Jones

Reputation: 4618

The character you're trying to replace isn't the same as the one in the name.

Best to remove all non alpha numeric characters instead, to cater for names such as:

  • O'Neill
  • St. Mary's

Try:

lastName.replace(/\W/g, '')

Upvotes: 8

Related Questions