Analysis of the conversion functions of the camper and character types implemented by JS, and js implements the camper

Source: Internet
Author: User

Analysis of the conversion functions of the camper and character types implemented by JS, and js implements the camper

The examples in this article describe how to use JavaScript to convert camps and hyphens. We will share this with you for your reference. The details are as follows:

I like this online section.

1. Camping to hyphen:

Var s = "fooStyleCss"; s = s. replace (/([A-Z])/g, "-$1 "). toLowerCase (); // Replace with regular expressions, which is concise and clear.

2. camper

var s1 = "foo-style-css";s1 = s1.replace(//-(/w)/g, function(all, letter){ return letter.toUpperCase();});

This section 2 is not very clear.

Write one by yourself, ^ _ ^, which is easy to understand, that is, the Code is a little more;

var s = "style-sheet-base";var a = s.split("-");var o = a[0];for(var i=1;i<a.length;i++){  o = o + a[i].slice(0,1).toUpperCase() + a[i].slice(1);}

Write another record. This time, use the regular expression:

var s1 = "style-sheet-base";s1 = s1.replace(//-(/w)/g, function(x){return x.slice(1).toUpperCase();});

Haha, interesting

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

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.