JavaScript string Method (ii)

Source: Internet
Author: User

String Case Conversion method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Case Conversion </title>
<body>
<script type= "Text/javascript" >
var str= "HELLO World";
Console.log (Str.tolowercase ());//hello World
Console.log (Str.touppercase ());//hello World
</script>
</body>
String pattern matching method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> string pattern matching </title>
<body>
<script type= "Text/javascript" >
/*
Match method: Accepts only one parameter, a regular expression specified by a string or RegExp object
Search method: Accepts only one parameter, a regular expression specified by a string or RegExp object
The search method returns the index of the first occurrence in a string, or 1 if there is no match.
*/
var str= "Cat,bat,sat,fat";
var pattern=/.at/;
var matches=str.match (pattern);
Console.log (matches.index);//0
Console.log (Matches[0]);//cat
Console.log (pattern.lastindex);//0
Lastindex represents the start of the search for the next occurrence of the character position, starting from 0
var pos=str.search (/at/);
Console.log (POS);//1 1 indicates where the at string first appears in the original string
</script>
</body>
Replace method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>replace Methods </title>
<body>
<script type= "Text/javascript" >
var str= "Cat,bat,sat,fat";
var res=str.replace ("At", "one");//The first argument is a string, so only the first substring is replaced
Console.log (res);//cone,bat,sat,fat

var res1=str.replace (/at/g, "one");//The first argument is a regular expression, so all substrings are replaced
Console.log (res1);//cone,bone,sone,fone
</script>
</body>
Split method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>split Methods </title>
<body>
<script type= "Text/javascript" >
/*
The Split method splits a string into a string array based on the specified character.
When the specified character is an empty string, the entire string is delimited
*/
var str= "Red,blue,green,yellow";
Console.log (Str.split (","));//["Red", "Blue", "green", "yellow"]
Console.log (Str.split (",", 2));//["Red", "blue"] the second parameter is used to limit the size of the array
Console.log (Str.split (/[^\,]+/));//[",", ",", ",", "," "]
The first and last entries are empty strings because the delimiter specified by the regular expression appears at the beginning of the substring, "red" and "yellow"
[^ ...] any character that is not in square brackets is a delimiter as long as it is not a comma
</script>
</body>
Localecompare method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>localecompare Methods </title>
<body>
<script type= "Text/javascript" >
/*
This method is used to compare two strings
1. If the string should precede the string parameter in the alphabet, a negative number is returned
1. If the string equals the string argument, 0 is returned.
1. Returns a positive number if the string should be in the alphabet after the string argument
*/
var str= "Yellow";
Console.log (Str.localecompare ("Brick"));//1
Console.log (Str.localecompare ("yellow"));//0
Console.log (Str.localecompare ("Zoo"));//-1
</script>
</body>

JavaScript string Method (ii)

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.