Analysis of the Hump-type and hyphen-transform function of JS _javascript skill

Source: Internet
Author: User
Tags regular expression

This paper illustrates the hump-type and hyphen-conversion function of JS. Share to everyone for your reference, specific as follows:

On the Internet to find this paragraph, more like

1. Hump Transfer character:

var s = "Foostylecss";
s = S.replace ([[A-z])/g, "-$1"). toLowerCase ();
Use regular to replace, concise and clear, very good

2. Turn Hump

var S1 = "Foo-style-css";
S1 = S1.replace (//-(/w)/g, function (all, letter) {return
 letter.touppercase ();
});

It's not quite clear what this 2 looks.

So I write a, ^_^, this is easy to understand, is the code a bit 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, this time with a regular:

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

Haha, interesting.

PS: Here again for you to provide 2 very convenient regular expression tools for your reference to use:

JavaScript Regular expression online test tool:
Http://tools.jb51.net/regex/javascript

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

More readers interested in JavaScript-related content can view the site topics: "JavaScript Replacement Operation Tips Summary", "JavaScript Search Algorithm Skills Summary", "JavaScript data structure and algorithm skills summary", " JavaScript traversal algorithm and tips Summary, "JavaScript in the JSON Operation tips Summary", "JavaScript Error and debugging skills summary" and "JavaScript Mathematical operation Summary"

I hope this article will help you with JavaScript programming.

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.