Home > Web Front-end > JS Tutorial > body text

Interpretation of examples of intercepting Chinese and English strings and punctuation marks without garbled characters using js_javascript skills

WBOY
Release: 2016-05-16 16:51:56
Original
1272 people have browsed it

Copy code The code is as follows:

<script> <br>function subString(str, len, hasDot) <br>{ <br>var newLength = 0; <br>var newStr = ""; <br>var chineseRegex = /[^x00-xff]/g; <br>var singleChar = ""; <br>var strLength = str.replace(chineseRegex,"**").length; <br>for(var i = 0;i < strLength;i ){ <BR>singleChar = str.charAt(i). toString(); <BR>if(singleChar.match(chineseRegex) != null){ <BR>newLength = 2; <BR>}else{ <BR>newLength ; <BR>} <BR>if(newLength > len){ <br>break; <br>} <br>newStr = singleChar; <br>} <br><br>if(hasDot && strLength > len){ <br>newStr = "..."; <br>} <br>return newStr; <br>} <br>alert(subString("js intercepts Chinese and English strings without garbled characters",10,true)); <br></script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template