Javascript three-character processing functions: Split join and replace replaceall

Source: Internet
Author: User

Let's take a look at the example. Copy the code into an HTML file and run it. Then we can understand it:

<SCRIPT Lanuage = "JavaScript">
VaR STR = "abcdemo-chicjkclm ";
VaR strsplitbyc = Str. Split ("C ");
VaR arrayjoinby3 = strsplitbyc. Join ("3 ");
Alert ("arrayjoinby3 =" + arrayjoinby3 );

VaR replaceone = Str. Replace ("C", "3 ");
Alert ("replaceone =" + replaceone );

VaR replaceall = Str. Replace (New Regexp ("C", "GM"), "3 ");
Alert ("replaceall =" + replaceall );
</SCRIPT>

If you run the command, the first output is the same as the third output. Yes, JavaScript does not provide the replaceall function, but to implement this function, it seems that I have provided two methods here! Welcome to the experts who passed by for advice. The younger brother is also a cainiao. Posting it here is just like taking a note. I used to disagree with this Js. I just got a taste of it recently. It seems that there are so many things to learn.

A good Web Development Learning address is also recommended here:W3C School

Split () method

 

Definition and usage

The split () method is used to split a string into a string array.

Syntax
stringObject.split(separator,howmany)
Parameters Description
Separator Required. String or regular expression, which separates stringobject from the specified place.
Howmany Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length.
Return Value

A string array. The array isSeparatorSplit the string stringobject into substrings at the specified boundary. The strings in the returned array do not includeSeparatorItself.

 

 

Replace () method

 

Definition and usage

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

Syntax
stringObject.replace(regexp,replacement)
Parameters Description
Regexp Required. Specifies the Regexp object of the pattern to be replaced. Note that if the value is a string, it is used as the direct text mode to be retrieved, rather than being converted to a Regexp object first.
Replacement Required. A string value. Specifies the function for replacing text or generating replacement text.
Return Value

A new string is obtained after the first match of Regexp or all matches are replaced by replacement.

 

 

 

 

 

 

 

Join () method

 

Definition and usage

The join () method is used to put all elements in the array into a string.

Elements are separated by the specified delimiter.

Syntax
arrayObject.join(separator)
Parameters Description
Separator Optional. Specifies the delimiter to use. If this parameter is omitted, a comma is used as the separator.
Return Value

Returns a string. This string is used to convert each element of arrayobject into a string, connect these strings, and insert them between two elements.SeparatorString.

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.