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 isU+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
1 Answer
Reset to default 8The 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
版权声明:本文标题:Javascript replacing single quote with empty string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744877576a2630009.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论