Summary of Chinese and space methods in the JavaScript filter string, javascript string
How does js filter Chinese characters or spaces in strings? There are many methods. We can use replacement and regular expressions to implement them. This article introduces two simple examples. If you are interested, refer to them.
1. javascript filter space:
function moveSpace() {var str = " abc defg";alert(str.replace(/[ ]/g, ""));}moveSpace();
2. Filter Chinese characters using javascript:
Var title = "string zifuchuan" var reg =/[u4E00-u9FA5]/g; var result = title. replace (reg, ''); alert (result );
3. Remove spaces at both ends of the string in javascript.
String.prototype.trim=function (){return this.replace(/(^/s*)|(/s*$)/g,'');}
4. javascript removes the string and therefore spaces.
String.prototype.trim=function (){return this.replace(/(^/s*)|(/s*$)/g,'');}
The above is a summary of the Chinese and space methods in the JavaScript filter string introduced by xiaobian. I hope it will help you!
Articles you may be interested in:
- Use javascript to filter html strings (annotation marking method)
- Solve Chinese garbled characters in Post and Request of JSP Through Filter
- JSP uses filters to solve Chinese garbled requests getParameter
- How to filter out img strings in java and javascript without displaying images
- JSP uses filters to solve Chinese request garbled characters