Summary of Chinese and space methods in the JavaScript filter string, javascript string

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.