Beyond Surrogate Pairs: Hidden Pitfalls in JS Character Counting with PHP/MySQL
dev.to·2h·
Discuss: DEV
📝Rope Editors
Preview
Report Post

When counting characters in JavaScript, the surrogate pair issue (where emoji and certain CJK characters count as 2) is fairly well-known. You’ve probably seen advice like "just use [...str].length" to solve it.

However, when I implemented a character counter for a textarea on the frontend, I discovered there are other causes of mismatches beyond surrogate pairs. It turned out to be deeper than I expected, so I’d like to share what I learned. I might have some things wrong, so please take this as a reference rather than absolute truth.

What Happened

I was implementing a character counter for a <textarea>. I was already using [...str].length as a surrogate pair countermeasure, but Laravel’s max validation was still rejecting some inputs.

After investigating, I foun…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help