JS Replace character substitution function

Source: Internet
Author: User

Today we talked about the character replacement function of JS replace the details, mainly about his replacement of simple characters and the use of regular expressions to deal with character replacement, there is a need for friends to see.

The simplest substitution function

<script language= "Web Effects" >
var strm = "JavaScript is a good script language";
Here I want to replace the letter A with the letter A
Alert (Strm.replace ("A", "a"));
</script>


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

Grammar
Stringobject.replace (regexp/substr,replacement) parameter description
Regexp/substr required. The RegExp object that prescribes the substring or pattern to be replaced.

Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object.

Replacement required. A string value. Provides a function that replaces text or generates alternate text.

<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
Alert (Strm.replace (/JavaScript) s* (IS)/g, "$ fun. It $"));
</script>


The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

<script language= "JavaScript" >
var strm = "JavaScript is a www.111cn.net good script language";
function Change (Word)
{
Return Word.indexof (0). toUpperCase () +word.substring (1);
}
Alert (Strm.replace (/bw+b/g,change));
</script>

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.