JavaScript string.replace () character substitution function

Source: Internet
Author: User

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.

Simple usage of one string.replace ()
var text = "Web page special effects are very powerful!" Text.replace (/javascript/i, "JavaScript"); 14//back: JavaScript is very powerful!

String.Replace () Replaces all occurrences of the target character
var text= "JavaScript is very powerful! JAVASCRIPT is one of my favorite languages! Text.replace (/javascript/ig, "JavaScript"); 18//back: JavaScript is very powerful! JavaScript is one of my favorite languages!

String.Replace () to implement the swap position

var name= "Doe, John";
Name.replace (/(w+) s*,s* (w+)/, "$ $");
Back: John Doe

String.Replace () implementation replaces all double quotes with characters enclosed in brackets
The var text = ' JavaScript ' is very powerful! ' Text.replace ([^ "]*)"/g, "[$]"); 26//return: [JavaScript] very powerful!

String.Replace () capitalize all characters first letter

More about Replace function usage please refer to

Http://www.111cn.net/wy/99/js_replace_replaceall.htm

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.