作者KC73 (肯先生)
看板Ajax
標題[ js ] 請教計算中/英文字數的問題..
時間Sat Feb 2 19:55:30 2008
請教諸位..
如果要即時檢查輸入文章的字數, 一般都用 elm.value.length 計算, 但是
這樣算法對英文不太公平...
This is an apple. <== 17
這是蘋果 <== 4
不知道有沒有前輩做過, 中文算字, 英文算 word (不是 letter) 的?
This is an apple. <== 4
這是蘋果 <== 4
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.166.139.236
→ gpmm:用 string.split (' ').length 02/02 20:05
推 TonyQ:可能要考慮分隔符號 XD 02/02 20:14
推 zi:用RegExp 例:var a = "中文測試 this is a word"; 02/03 19:00
→ zi:a.match(/[\u4E00-\u9FA5]|\w+/g).length 02/03 19:01
→ KC73:感謝, 在下試試看囉~ 02/07 03:52