JS replace () method-capitalize the first letter of the string

Source: Internet
Author: User

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

The replace () method has two parameters, the first parameter is a regular expression, and the regular expression, if with the global flag/g, represents the replacement of all matching strings, or only the first matching string is replaced.
The second argument can be a string, or it can be a function. $, $ ... represents text that matches a regular expression.

There is many ways we can make a difference. Global change starts with you. Sign up for our free newsletter today.

Output: There is many Ways We Can make A difference. Global change starts with you. Sign up for our free Newsletter Today.

The source code is as follows:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"Content= "text/html; charset=utf-8;">5 <title>JS Replace method</title>6 <Metaname= "Author"content= "Rainna" />7 <Metaname= "keywords"content= "Rainna S js Lib" />8 <Metaname= "description"content= "JS Replace method" />9 </Head>Ten <Body> One <H1>Use the JS replace () method to capitalize the first letter of all words</H1> A <P>The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.<BR/>The replace () method has two parameters, the first parameter is a regular expression, and the regular expression, if with the global flag/g, represents the replacement of all matching strings, or only the first matching string is replaced.<BR/> -The second argument can be a string, or it can be a function. $, $ ... represents text that matches a regular expression.</P> - <PID= "word">There is many ways we can make a difference. Global change starts with you. Sign up for our free newsletter today.</P> the <ahref=""ID= "Replacebtn">Replace</a> -  - <Script> - varReplacefunc= function(){ +     varWord=document.getElementById ('Word'). Innertext.tostring (), - btn=document.getElementById ('replacebtn'); +      A     varfunc1= function(str) { at         returnStr.replace (/\b\w+\b/G,function(word) { -              returnword.substring (0,1). toUpperCase ()+word.substring (1); -         }); -     } -      - Btn.onclick= function(event) { in Event.preventdefault (); - Console.log (func1 (Word)); to     }     + } -  the Replacefunc (); * </Script> $ </Body>Panax Notoginseng </HTML>

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.