admin管理员组

文章数量:1404923

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:

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

What am I doing wrong?

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:

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

What am I doing wrong?

Share Improve this question asked Apr 7, 2017 at 15:06 Ali KhakpouriAli Khakpouri 8759 silver badges31 bronze badges 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. – gen_Eric Commented Apr 7, 2017 at 15:09
  • console.log(lastName) outputs: O'Donnell as intended on chrome for me. Maybe try a different browser ? – user2202098 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. – gen_Eric 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. – Ali Khakpouri Commented Apr 7, 2017 at 15:25
Add a ment  | 

1 Answer 1

Reset to default 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, '')

本文标签: Javascript replacing single quote with empty stringStack Overflow