1

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?

4
  • 4
    That's not a ' character, it's actually a "smart quote" character. It's unicode point is U+2019. You can try to use the regex /\u2019/g. Commented Apr 7, 2017 at 15:09
  • console.log(lastName) outputs: O'Donnell as intended on chrome for me. Maybe try a different browser ? Commented Apr 7, 2017 at 15:12
  • @user2202098: The screenshot shows that it's not a ' character. He must the smart quote in his code (if you copy and paste from MS word, it sometimes does this). Also, those characters don't always stay when copying and pasting into other programs or as plain text. Commented Apr 7, 2017 at 15:13
  • I didn't. I specifically used '. My local dev site displays a different text than the client's dev site. Even though, the source code is the same. Commented Apr 7, 2017 at 15:25

1 Answer 1

8

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, '')
Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thank you so much.

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.