JavaScript replace () replaces all usages

Source: Internet
Author: User
Tags function prototype regular expression

String.Replace () Introduction

Grammar:

String.Replace (RegExp, replacement)

RegExp: The regular expression for which you want to perform the substitution operation. If a string is passed in, it is treated as a normal character, and only one substitution operation is performed, and if the regular expression is present with the global (g) modifier, all occurrences of the target character are replaced. Only one substitution operation will be performed.

Replacement: The character you want to replace.

The simplest example

The code is as follows Copy Code

<script type= "Text/javascript" >

var str= "Visit microsoft!";
document.write (Str.replace ("Microsoft", "W3Schools"));

</script>

The return value is the string after the substitution operation.

Simple usage of string.replace ()

The code is as follows Copy Code

<script type= "Text/javascript" >

var str= "Visit microsoft!";
document.write (Str.replace (/microsoft/i, "W3Schools"));

</script>

String.Replace () Replaces all occurrences of the target character

var text= "JavaScript is very powerful! JAVASCRIPT is one of my favorite languages! ";

The code is as follows Copy Code

Text.replace (/javascript/ig, "JavaScript"); Return:

JavaScript is very powerful! JavaScript is one of my favorite languages!

String.Replace () to implement the swap position

The code is as follows Copy Code

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 code is as follows Copy Code

The var text = ' JavaScript ' is very powerful! ';

Text.replace ([^ "]*)"/g, "[$]"); Back: [JavaScript] very powerful!

String.Replace () capitalize all characters first letter

The code is as follows Copy Code

var text = ' A journey of a thousand miles begins with single step. '

Text.replace (/bw+b/g, function (word) {

Return word.substring (0,1). toUpperCase () + word.substring (1);

});

Return: A Journey of a thousand Miles begins with single step.

The replace () method in JavaScript replaces the first matching character directly with str.replace ("Apples", "oranges").

Replaces all matching characters with/g,/I ignores case

The code is as follows Copy Code

<SCRIPT>
str = "Apples are round, and Apples are juicy.";
Newstr=str.replace (/apples/gi, "oranges");
document.write (NEWSTR)
</SCRIPT> This prints "oranges are round, and oranges are juicy."

JavaScript's string-Class built-in function replace (regexp, newstring) function provides a string substitution function, which can be seen from the function prototype to support Regular EXP. This function is very useful, but there are a few places where there is a bit of confusion, the following is illustrated by the actual example:
For example, we now want to target the string

The code is as follows Copy Code
var src= "<a href=" http://xx.com/a/20100601/000224.htm "target=" _blank ">xxx</a>"

To replace the HTML double quotes in which the transfer character is "real", normal we will call Src.replace (/"/g, '") directly; to achieve the goal, but JS easy to confuse is that this does not actually change the value of SRC, The actual test found that the return value is the result we need, and SRC itself does not change, so in practice we need to do the following to complete our functions:

  code is as follows copy code
src= Src.replace (/"/g, '");
Related Article

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.