Home >Web Front-end >JS Tutorial >Interpretation of examples of intercepting Chinese and English strings and punctuation marks without garbled characters using js_javascript skills

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:51:561337browse

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>
Statement:
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